www.pudn.com > main04.rar > mainwindow.cpp, change:2016-03-31,size:19311b


#include "mainwindow.h" 
#include "ui_mainwindow.h" 
#include<QInputDialog> 
#include <QFileDialog> 
#include <QFile> 
#include <QTextStream> 
#include <QDebug> 
#include<QVector> 
#include<QPen> 
void *alloc1 (int n1, int size); 
float *alloc1float(int n1); 
 
MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    QStandardItemModel* model = new QStandardItemModel(ui->treeView); 
    //model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("项目名")<<QStringLiteral("信息")); 
    model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("项目名")); 
    QStandardItem*itemProject = new QStandardItem(QIcon("1.png"),QStringLiteral("项目")); 
    itemProject->setCheckable(true); 
    itemProject->setTristate(true); 
    model->appendRow(itemProject); 
 
    //设置表格显示数据 
    model_table = new QStandardItemModel(4,8); 
    model_table->setHeaderData(0,Qt::Horizontal,tr("001")); 
    model_table->setHeaderData(1,Qt::Horizontal,tr("002")); 
    model_table->setHeaderData(2,Qt::Horizontal,tr("003")); 
    model_table->setHeaderData(3,Qt::Horizontal,tr("004")); 
    model_table->setHeaderData(4,Qt::Horizontal,tr("005")); 
    model_table->setHeaderData(5,Qt::Horizontal,tr("006")); 
    model_table->setHeaderData(6,Qt::Horizontal,tr("007")); 
    model_table->setHeaderData(7,Qt::Horizontal,tr("008")); 
    setupView(); 
 
    // 
 
    //设置鼠标右键菜单 
    this->ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu); 
    connect(ui->treeView, SIGNAL(customContextMenuRequested(const QPoint)), this, SLOT(ShowMouseRightButton(const QPoint))); 
 
    // 
    connect(model,&QStandardItemModel::itemChanged,this,&MainWindow::treeItemChanged);//实现半选择,全选,不选状态 
    ui->treeView->setModel(model); 
    // 画图 
    //plotcurves(); 
    //connect(ui->plot_Bn,SIGNAL(clicked(bool)),this,SLOT(plotcurves())); 
 
 
} 
 
MainWindow::~MainWindow() 
{ 
    delete ui; 
} 
void MainWindow::setupView() 
{ 
 
    ui->tableView->setModel(model_table); 
 
    QItemSelectionModel *selectionModel=new QItemSelectionModel(model_table); 
    ui->tableView->setSelectionModel(selectionModel); 
    connect(selectionModel,SIGNAL(selectionChanged(QItemSelection,QItemSelection)),ui->tableView,SLOT(selectionChanged(QItemSelection,QItemSelection))); 
 
 
} 
void MainWindow::on_treeView_clicked(const QModelIndex &index) 
{ 
    QString str; 
     str += QStringLiteral("当前选中:%1\nrow:%2,column:%3\n").arg(index.data().toString()) 
                           .arg(index.row()).arg(index.column()); 
     str += QStringLiteral("父级:%1\n").arg(index.parent().data().toString()); 
     //ui->label_realTime->setText(str); 
     // 
     QString name,info; 
         if(index.column() == 0) 
         { 
             name = index.data().toString(); 
             info = index.sibling(index.row(),1).data().toString(); 
         } 
         else 
         { 
             name = index.sibling(index.row(),0).data().toString(); 
             info = index.data().toString(); 
         } 
         str += QStringLiteral("名称:%1\n信息:%2\n").arg(name).arg(info); 
 
         //根据任意节点信息找到其最后的父级节点 
         QString top = getTopParent(index).data().toString(); 
         str += QStringLiteral("顶层节点名:%1\n").arg(top); 
         ui->label_realTime->setText(str); 
} 
QStandardItem* MainWindow::getTopParent(QStandardItem* item) 
{ 
    QStandardItem* secondItem = item; 
    while(item->parent()!= 0) 
    { 
        secondItem = item->parent(); 
        item = secondItem; 
    } 
    if(secondItem->index().column() != 0) 
    { 
         QStandardItemModel* model = static_cast<QStandardItemModel*>(ui->treeView->model()); 
         secondItem = model->itemFromIndex(secondItem->index().sibling(secondItem->index().row(),0)); 
    } 
    return secondItem; 
} 
QModelIndex MainWindow::getTopParent(QModelIndex itemIndex) 
{ 
    QModelIndex secondItem = itemIndex; 
    while(itemIndex.parent().isValid()) 
    { 
        secondItem = itemIndex.parent(); 
        itemIndex = secondItem; 
    } 
    if(secondItem.column() != 0) 
    { 
         secondItem = secondItem.sibling(secondItem.row(),0); 
    } 
    return secondItem; 
} 
void MainWindow::treeItemChanged( QStandardItem * item ) 
{ 
    if ( item == Q_NULLPTR ) 
    return ; 
    if ( item ->isCheckable ()) 
    { 
        //如果条目是存在复选框的,那么就进行下面的操作 
        Qt::CheckState state = item ->checkState (); //获取当前的选择状态 
        if ( item -> isTristate ()) 
        { 
             //如果条目是三态的,说明可以对子目录进行全选和全不选的设置 
            if ( state != Qt::PartiallyChecked ) 
            { 
                //当前是选中状态,需要对其子项目进行全选 
                treeItem_checkAllChild ( item , state == Qt::Checked ? true : false ); 
            } 
        } 
        else 
        { 
            //说明是两态的,两态会对父级的三态有影响 
            //判断兄弟节点的情况 
            treeItem_CheckChildChanged ( item ); 
        } 
    } 
} 
// 
// \brief 递归设置所有的子项目为全选或全不选状态 
// \param item 当前项目 
// \param check true时为全选,false时全不选 
// 
void MainWindow::treeItem_checkAllChild(QStandardItem * item, bool check) 
{ 
    if(item == Q_NULLPTR) 
        return; 
    int rowCount = item->rowCount(); 
    for(int i=0;i<rowCount;++i) 
    { 
        QStandardItem* childItems = item->child(i); 
        treeItem_checkAllChild_recursion(childItems,check); 
    } 
    if(item->isCheckable()) 
        item->setCheckState(check ? Qt::Checked : Qt::Unchecked); 
} 
void MainWindow::treeItem_checkAllChild_recursion(QStandardItem * item,bool check) 
{ 
    if(item == Q_NULLPTR) 
        return; 
    int rowCount = item->rowCount(); 
    for(int i=0;i<rowCount;++i) 
    { 
        QStandardItem* childItems = item->child(i); 
        treeItem_checkAllChild_recursion(childItems,check); 
    } 
    if(item->isCheckable()) 
        item->setCheckState(check ? Qt::Checked : Qt::Unchecked); 
} 
// 
// \brief 根据子节点的改变,更改父节点的选择情况 
// \param item 
// 
void MainWindow::treeItem_CheckChildChanged(QStandardItem * item) 
{ 
    if(Q_NULLPTR == item) 
        return; 
    Qt::CheckState siblingState = checkSibling(item); 
    QStandardItem * parentItem = item->parent(); 
    if(Q_NULLPTR == parentItem) 
        return; 
    if(Qt::PartiallyChecked == siblingState) 
    { 
        if(parentItem->isCheckable() && parentItem->isTristate()) 
            parentItem->setCheckState(Qt::PartiallyChecked); 
    } 
    else if(Qt::Checked == siblingState) 
    { 
        if(parentItem->isCheckable()) 
            parentItem->setCheckState(Qt::Checked); 
    } 
    else 
    { 
        if(parentItem->isCheckable()) 
            parentItem->setCheckState(Qt::Unchecked); 
    } 
    treeItem_CheckChildChanged(parentItem); 
} 
// \brief 测量兄弟节点的情况,如果都选中返回Qt::Checked,都不选中Qt::Unchecked,不完全选中返回Qt::PartiallyChecked 
// \param item 
// \return 如果都选中返回Qt::Checked,都不选中Qt::Unchecked,不完全选中返回Qt::PartiallyChecked 
// 
 
Qt::CheckState MainWindow::checkSibling(QStandardItem * item) 
{ 
    //先通过父节点获取兄弟节点 
    QStandardItem * parent = item->parent(); 
    if(Q_NULLPTR == parent) 
        return item->checkState(); 
    int brotherCount = parent->rowCount(); 
    int checkedCount(0),unCheckedCount(0); 
    Qt::CheckState state; 
    for(int i=0;i<brotherCount;++i) 
    { 
        QStandardItem* siblingItem = parent->child(i); 
        state = siblingItem->checkState(); 
        if(Qt::PartiallyChecked == state) 
            return Qt::PartiallyChecked; 
        else if(Qt::Unchecked == state) 
            ++unCheckedCount; 
        else 
            ++checkedCount; 
        if(checkedCount>0 && unCheckedCount>0) 
            return Qt::PartiallyChecked; 
    } 
    if(unCheckedCount>0) 
        return Qt::Unchecked; 
    return Qt::Checked; 
} 
void MainWindow ::ShowMouseRightButton(const QPoint& pos) 
{ 
       QMenu *qMenu = NULL; 
 
       if (qMenu) 
       { 
               delete qMenu;//及时释放Qmenu,保证只有一个Qmenu 
               qMenu = NULL; 
       } 
 
       qMenu = new QMenu(ui->treeView); 
 
 
       QAction* addproAction = new QAction("添加新项目",this); 
       connect(addproAction, SIGNAL(triggered()), this, SLOT(addpro())); 
 
       QAction* closePaneAction = new QAction("添加文件夹",this); 
       connect(closePaneAction, SIGNAL(triggered()), this, SLOT(addtree())); 
 
       QAction* addTreeItemAction = new QAction("添加文件", this); 
       //connect(addTreeItemAction, SIGNAL(triggered()), this, SLOT(AddTreeItem())); 
       connect(addTreeItemAction, SIGNAL(triggered()), this, SLOT(slotOpen())); 
 
       qMenu->addAction(addproAction); 
       qMenu->addAction(closePaneAction); 
       qMenu->addAction(addTreeItemAction); 
 
       qMenu->exec(QCursor::pos()); //在鼠标点击的位置显示鼠标右键菜单 
 
} 
void MainWindow ::addpro() 
{ 
    QStandardItemModel* model = static_cast<QStandardItemModel*>(ui->treeView->model()); 
    QStandardItem*itemProject = new QStandardItem(QIcon("1.png"),QStringLiteral("项目")); 
    itemProject->setCheckable(true); 
    itemProject->setTristate(true); 
    model->appendRow(itemProject); 
    //model->setItem(model->indexFromItem(itemProject).row(),1,new QStandardItem(QStringLiteral("项目信息说明"))); 
} 
 
void MainWindow ::addtree() 
{ 
    QStandardItemModel* model = static_cast<QStandardItemModel*>(ui->treeView->model()); 
    QModelIndex index=ui->treeView->currentIndex(); 
    QStandardItem *currentItem = model->itemFromIndex(index); 
    QStandardItem *itemFolder = new QStandardItem(QIcon("FILE_01.jpg"),QStringLiteral("文件夹1")); 
    itemFolder->setCheckable(true);//复选框定义 
    itemFolder->setTristate(true);//三种状态定义 
    currentItem->appendRow(itemFolder); 
    //currentItem->setChild(itemFolder->index().row(),1,new QStandardItem(QStringLiteral("信息说明"))); 
 
} 
void MainWindow ::AddTreeItem() 
{ 
    QStandardItemModel* model = static_cast<QStandardItemModel*>(ui->treeView->model()); 
    QModelIndex index=ui->treeView->currentIndex(); 
    QStandardItem *currentItem = model->itemFromIndex(index); 
    QStandardItem *itemfile = new QStandardItem(QIcon("signal.jpg"),QStringLiteral("文件001")); 
    itemfile->setCheckable(true);//复选框定义 
    currentItem->appendRow(itemfile); 
    //currentItem->setChild(itemfile->index().row(),1,new QStandardItem(QStringLiteral("信息说明"))); 
} 
 
 void MainWindow::slotOpen() 
{ 
    QString name; 
    //name = QFileDialog::getOpenFileName(this,"OPEN",".","histogram files (*.dat)"); 
    name = QFileDialog::getOpenFileName(this); 
    if (!name.isEmpty()) 
        openFile(name); 
    // 
    QString  file_name_textview, file_path_textview; 
    QString file_absopath_textview; 
    QFileInfo fi_textview; 
    fi_textview = QFileInfo(name); 
    file_name_textview = fi_textview.fileName();//文件名 
    file_path_textview = fi_textview.absolutePath();//文件路径 
    file_absopath_textview=fi_textview.filePath();//文件绝对路径 
    //显示文件目录 
    ui->lineEdit_file->setText(file_path_textview); 
    //在新工区下新建输入文件的列表 
    // 将外部输入的文件名放在指定的父节点下 
    QStandardItemModel* model = static_cast<QStandardItemModel*>(ui->treeView->model()); 
    QModelIndex index=ui->treeView->currentIndex(); 
    QStandardItem *currentItem = model->itemFromIndex(index); 
    //QStandardItem *itemfile = new QStandardItem(QIcon("signal.jpg"),QStringLiteral("文件001")); 
    QStandardItem *itemfile = new QStandardItem(QIcon("signal.jpg"),file_name_textview); 
    itemfile->setCheckable(true);//复选框定义 
    currentItem->appendRow(itemfile); 
    //currentItem->setChild(itemfile->index().row(),1,new QStandardItem(QStringLiteral("信息说明"))); 
 
 
} 
 void MainWindow::openFile(QString path) 
 { 
     int row_file; 
     if (!path.isEmpty()) 
     { 
         QFile file(path); 
         if (file.open(QFile::ReadOnly|QFile::Text)) 
         { 
             QTextStream stream(&file); 
             QString line; 
 
             model_table->removeRows(0,model_table->rowCount(QModelIndex()),QModelIndex()); 
             int row = 0; 
             do 
             { 
                 line = stream.readLine(); 
                 QStringList strs = line.split(QRegExp("\\s+"),QString::SkipEmptyParts); 
                 //qDebug()<<"The 速度数据 columns is m "<<strs.size(); 
                 int m=strs.size(); 
                 //qDebug()<<"加载数据的列数为"<<m; 
                 if (!line.isEmpty()) 
                 { 
                     model_table->insertRows(row, 1, QModelIndex()); 
                     QStringList pieces=line.split(QRegExp("\\s+"),QString::SkipEmptyParts); 
                     for (int k=0;k<m;k++) 
                     { 
                     model_table->setData(model_table->index(row, k, QModelIndex()),pieces.value(k)); 
                      } 
                     row++; 
                 } 
             }while (!line.isEmpty()); 
        row_file=row; 
             //将数据输入数组并存放 
 
 
         } 
         file.close(); 
     } 
     qDebug()<<"行数"<<row_file; 
 
     QFile file01(path); 
     QTextStream ts(&file01); 
     float *x,*y; 
     x=alloc1float(row_file); 
     y=alloc1float(row_file); 
     //QVector<float> X(row_file); 
 
     //QVector<float> Y(row_file); 
 
 
     if (!file01.open(QIODevice::ReadOnly|QIODevice::Text)) 
           { 
               qDebug()<<"can't open the file!"<<endl; 
               return; 
           } 
     for(int i=0;i<row_file;i++) 
     { 
         ts>>y[i]>>x[i]; 
         qDebug()<<"00"<<y[i]<<"00"<<x[i]; 
         X.push_back(x[i]); 
         Y.push_back(y[i]); 
         qDebug()<<"010"<<Y[i]<<"010"<<X[i]; 
     } 
     file01.close(); 
 } 
 void MainWindow::on_plot_Bn_clicked()//画图按钮响应 
 { 
     int x_max; 
     x_max=X[X.size()-1]; 
     int y_max; 
     y_max=Y[Y.size()-1]; 
    // plotcurves(); 
     //for (int i=0;i<X.size();i++) 
         qDebug()<<"ppppp"<<X.size(); 
     // 
     QVector<double> x(X.size()),y(X.size()); 
        for (int k=0;k<X.size();k++) 
     { 
        x[k] = X.at(k); 
        y[k] = Y.at(k); 
     } 
     //设置属性可缩放,移动等 
         ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | 
                                          QCP::iSelectLegend | QCP::iSelectPlottables); 
         //设置坐标轴标签名称 
         ui->customPlot->xAxis->setLabel("x"); 
         ui->customPlot->yAxis->setLabel("y"); 
         ui->customPlot->legend->setVisible(true); 
         //  ui->customPlot->axisRect()->setupFullAxesBox(); 
         ui->customPlot->addGraph();//向绘图区域QCustomPlot(从widget提升来的)添加一条曲线 
         ui->customPlot->graph(0)->setName("曲线1");//曲线名称 
         ui->customPlot->graph(0)->setData(x,y); //设置曲线数据 
 
         //设置坐标轴显示范围,否则我们只能看到默认的范围 
         ui->customPlot->xAxis->setRange(0,x_max); 
         ui->customPlot->yAxis->setRange(0,y_max); 
         //ui->customPlot->xAxis->setRange(a1,b1); 
         //ui->customPlot->yAxis->setRange(a2,b2); 
 
          //重绘,这里可以不用,官方例子有,执行setData函数后自动重绘 
          //我认为应该用于动态显示或者是改变坐标轴范围之后的动态显示,我们以后探索 
 
          //设置曲线颜色 
           QPen pen; 
           pen.setWidth(1); 
           pen.setColor(Qt::blue);// line color blue for first graph 
           ui->customPlot->graph(0)->setPen(pen); 
           //ui->customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue 
           ui->customPlot->replot(); 
 
 } 
 
void MainWindow::plotcurves() 
{ 
    QVector<double> x(100),y(100);//分别存放x和y坐标的数据,100为数据长度 
        //添加数据,我们这里演示y=x^3,为了正负对称,我们x从-10到+10 
        for(int i=0;i<100;i++) 
        { 
            x[i] = i/5 -10; 
            y[i] = x[i] * x[i] * x[i]; 
        } 
            //设置属性可缩放,移动等 
                ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | 
                                                 QCP::iSelectLegend | QCP::iSelectPlottables); 
                //设置坐标轴标签名称 
                ui->customPlot->xAxis->setLabel("x"); 
                ui->customPlot->yAxis->setLabel("y"); 
                ui->customPlot->legend->setVisible(true); 
                //  ui->customPlot->axisRect()->setupFullAxesBox(); 
                ui->customPlot->addGraph();//向绘图区域QCustomPlot(从widget提升来的)添加一条曲线 
                ui->customPlot->graph(0)->setName("曲线1");//曲线名称 
                ui->customPlot->graph(0)->setData(x,y); //设置曲线数据 
 
                //设置坐标轴显示范围,否则我们只能看到默认的范围 
                ui->customPlot->xAxis->setRange(-11,11); 
                ui->customPlot->yAxis->setRange(-1100,1100); 
                //ui->customPlot->xAxis->setRange(a1,b1); 
                //ui->customPlot->yAxis->setRange(a2,b2); 
 
                    //重绘,这里可以不用,官方例子有,执行setData函数后自动重绘 
                    //我认为应该用于动态显示或者是改变坐标轴范围之后的动态显示,我们以后探索 
 
                    //设置曲线颜色 
                        QPen pen; 
                        pen.setWidth(5); 
                        pen.setColor(Qt::blue);// line color blue for first graph 
                        ui->customPlot->graph(0)->setPen(pen); 
                        ui->customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue 
                        ui->customPlot->addGraph();//添加第二条曲线 
                        ui->customPlot->graph(1)->setName("曲线2");//曲线名称 
                        ui->customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph 
                       for(int i=0;i<100;i++) 
                        { 
                            y[i] = y[i]+100; 
                        } 
 
                       ui->customPlot->graph(1)->setData(x,y); 
                       ui->customPlot->replot(); 
 
 
 
 
} 
//  /////////////////////////////////////////////////////////// 
void *alloc1 (int n1, int size) 
{ 
    void *p; 
 
    if ((p=malloc(n1*size))==NULL) 
        return NULL; 
    return p; 
} 
float *alloc1float(int n1) 
{ 
    return (float*)alloc1(n1,sizeof(float)); 
}