Advertisement
Guest User

Qt Project Forum source code

a guest
Jul 7th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.09 KB | None | 0 0
  1. main.h:
  2.  
  3. #include <QDebug>
  4.  
  5. #include <QtWidgets/QLabel>
  6. #include <QtWidgets/QTreeWidgetItem>
  7. #include <QtWidgets/QListWidgetItem>
  8. #include <QtWidgets/QTableView>
  9. #include <QtWidgets/QLineEdit>
  10. #include <QtWidgets/QTextEdit>
  11.  
  12. class QTreeWidget;
  13. class QTreeWidgetItem;
  14. class QListWidget;
  15. class QListWidgetItem;
  16.  
  17. #include <QHBoxLayout>
  18. #include <QScrollArea>
  19. #include <QHeaderView>
  20. #include <QDialog>
  21. #include <QPushButton>
  22. #include <QDialogButtonBox>
  23.  
  24. #include <QApplication>
  25. #include <QMainWindow>
  26. #include <QAbstractTableModel>
  27. #include <QSortFilterProxyModel>
  28. //#include <QAbstractItemModel>
  29. #include <QBrush>
  30. #include <QFont>
  31.  
  32. // ===============================================================================================
  33.  
  34. class DataEntity
  35. {
  36. public:
  37.   DataEntity();
  38.   virtual ~DataEntity();
  39.   const QString getName() const { return name; };
  40.   const QString getDescription() const { return description; };
  41.   bool setData(const QString _name,
  42.                const QString _description);
  43. private:
  44.  
  45.   QString name;
  46.   QString description;
  47.   /* more data ... */
  48. };
  49.  
  50. // -----------------------------------------------------------------------------------------------
  51.  
  52. class ListModel : public QAbstractTableModel
  53. {
  54.   Q_OBJECT
  55.  
  56. public:
  57.   explicit ListModel(QObject *parent=0, QList<DataEntity>*local_list=0, QList<DataEntity>*global_list=0);
  58.   ~ListModel();
  59.  
  60.   Qt::ItemFlags flags(const QModelIndex &index) const;
  61.   QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
  62.   int rowCount(const QModelIndex &index=QModelIndex()) const;
  63.   int columnCount(const QModelIndex &index=QModelIndex()) const;
  64.  
  65.   DataEntity *getItem(const QModelIndex &index);
  66.   void deleteItem(const QModelIndex &index);
  67.  
  68.   void reset() { beginResetModel(); endResetModel(); };
  69.  
  70. private:
  71.   int listCount() const;
  72.  
  73.   QList<DataEntity> *local_list;
  74.   QList<DataEntity> *global_list;
  75. };
  76.  
  77. // -----------------------------------------------------------------------------------------------
  78.  
  79. class ProxyModel : public QSortFilterProxyModel
  80. {
  81.   Q_OBJECT
  82.  
  83. public:
  84.     explicit ProxyModel(QObject *parent=0);
  85.  
  86.     QString getSubStr() const { return substr; }
  87.  
  88.     void reset() { beginResetModel(); endResetModel(); };
  89.  
  90. public slots:
  91.     void clearFilters();
  92.     void setSubStr(const QString &new_substr);
  93.  
  94. protected:
  95.     bool filterAcceptsRow(int sourceRow,
  96.                           const QModelIndex &sourceParent) const;
  97.  
  98. private:
  99.     QString substr;
  100. };
  101.  
  102. // -----------------------------------------------------------------------------------------------
  103.  
  104. class DetailsDialog : public QDialog
  105. {
  106.   Q_OBJECT
  107. public:
  108.   explicit DetailsDialog(QWidget *parent = 0);
  109.   ~DetailsDialog();
  110.  
  111.   void setFormValues(DataEntity * const data);
  112.   bool getFormValues(DataEntity * const data);
  113.  
  114. private:
  115.   QLineEdit *lineEdit_name;
  116.   QTextEdit *textEdit_description;
  117. };
  118.  
  119. // -----------------------------------------------------------------------------------------------
  120.  
  121. class MainWindow : public QMainWindow
  122. {
  123.   Q_OBJECT
  124. public:
  125.   explicit MainWindow(QWidget *parent = 0);
  126.   ~MainWindow();
  127.  
  128. private slots:
  129.   // Main-Lists entry modify functions
  130.   void editListItem(const QModelIndex*);
  131.   void addListEntry();
  132.   void editListEntry(const QModelIndex&);
  133.   void deleteListEntry();
  134.  
  135. private:
  136.   /* GUI selection lists */
  137.   ListModel *model;
  138.   ProxyModel *proxyModel;
  139.  
  140.   /* Data */
  141.   QList<DataEntity> local;
  142. };
  143.  
  144. main.cpp:
  145.  
  146. // -----------------------------------------------------------------------------------------------
  147. #include "main.h"
  148. // -----------------------------------------------------------------------------------------------
  149.  
  150. // Global list
  151. QList<DataEntity> global;
  152.  
  153. // ===============================================================================================
  154.  
  155. DataEntity::DataEntity()
  156. {
  157.   name = QString("Entry %1").arg(rand()%1000);
  158.   description = QString();
  159. }
  160.  
  161. DataEntity::~DataEntity() { }
  162.  
  163. bool DataEntity::setData(const QString _name,
  164.                            const QString _description)
  165. {
  166.   // Set the values
  167.   name = _name;
  168.   description = _description;
  169.   return true;
  170. }
  171.  
  172. // ===============================================================================================
  173.  
  174. ListModel::ListModel(QObject *parent, QList<DataEntity>*_local_list, QList<DataEntity>*_global_list) : QAbstractTableModel(parent)
  175. {
  176.   local_list = _local_list;
  177.   global_list = _global_list;
  178. }
  179.  
  180. ListModel::~ListModel() { }
  181.  
  182. int ListModel::listCount() const
  183. {
  184.   int totalCount = 0;
  185.   if (local_list) totalCount += local_list->count();
  186.   if (global_list) totalCount += global_list->count();
  187.   return totalCount;
  188. }
  189.  
  190. Qt::ItemFlags ListModel::flags(const QModelIndex &index) const
  191. {
  192.   Qt::ItemFlags theFlags = QAbstractTableModel::flags(index);
  193.   if (index.isValid())
  194.     theFlags |= Qt::ItemIsSelectable | Qt::ItemIsDragEnabled |
  195.         Qt::ItemIsEnabled;
  196.   return theFlags;
  197. }
  198.  
  199. QVariant ListModel::data(const QModelIndex &index, int role) const
  200. {
  201.   if (!index.isValid() ||
  202.       index.row() < 0 || index.row() >= listCount() ||
  203.       index.column() < 0 || index.column() >= 1)
  204.     return QVariant();
  205.  
  206.   const DataEntity *item = NULL;
  207.   int offset = 0;
  208.   bool is_global = false;
  209.  
  210.   if (local_list)
  211.   {
  212.     offset = local_list->count();
  213.     if (index.row() < offset) item = &local_list->at(index.row());
  214.   }
  215.  
  216.   if (!item && global_list && index.row()-offset < global_list->count())
  217.   {
  218.     item = &global_list->at(index.row()-offset);
  219.     is_global = true;
  220.   }
  221.  
  222.   if (role == Qt::DisplayRole || role == Qt::EditRole)
  223.   {
  224.     switch (index.column())
  225.     {
  226.       case 0: return item->getName();
  227.       default: Q_ASSERT(false);
  228.     }
  229.   }
  230.   else if (role == Qt::FontRole)
  231.   {
  232.     if (is_global) { QFont font; font.setBold(true); return font; }
  233.   }
  234.   else if (role == Qt::UserRole)
  235.   {
  236.     return item->getName();
  237.   }
  238.  
  239.   return QVariant();
  240. }
  241.  
  242. int ListModel::rowCount(const QModelIndex &index) const
  243. {
  244.   return index.isValid() ? 0 : listCount();
  245. }
  246.  
  247. int ListModel::columnCount(const QModelIndex &index) const
  248. {
  249.   return index.isValid() ? 0 : 1;
  250. }
  251.  
  252. DataEntity *ListModel::getItem(const QModelIndex &index)
  253. {
  254.   if (!index.isValid() ||
  255.       index.row() < 0 || index.row() >= listCount())
  256.     return 0;
  257.  
  258.   DataEntity *item = NULL;
  259.   int offset = 0;
  260.  
  261.   if (local_list)
  262.   {
  263.     offset = local_list->count();
  264.     if (index.row() < offset) item = (DataEntity *)&local_list->at(index.row());
  265.   }
  266.  
  267.   if (!item && global_list && index.row()-offset < global_list->count())
  268.   {
  269.     item = (DataEntity *)&global_list->at(index.row()-offset);
  270.   }
  271.  
  272.   return item;
  273. }
  274.  
  275. void ListModel::deleteItem(const QModelIndex &index)
  276. {
  277.   if (!index.isValid() ||
  278.       index.row() < 0 || index.row() >= listCount())
  279.     return;
  280.  
  281.   int offset = 0;
  282.  
  283.   beginResetModel();
  284.   //beginRemoveRows(index, index.row(), index.row()); // Should this work too?
  285.  
  286.   if (local_list)
  287.   {
  288.     offset = local_list->count();
  289.     if (index.row() < offset)
  290.     {
  291.       // Delete from local list
  292.       local_list->removeAt(index.row());
  293.       goto done;
  294.     }
  295.   }
  296.  
  297.   if (global_list && index.row()-offset < global_list->count())
  298.   {
  299.     // Delete from global list
  300.     global_list->removeAt(index.row()-offset);
  301.     goto done;
  302.   }
  303.  
  304.  done:
  305.   //endRemoveRows();
  306.   endResetModel();
  307. }
  308.  
  309. // ===============================================================================================
  310.  
  311. ProxyModel::ProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
  312. {
  313.   substr.clear();
  314. }
  315.  
  316. void ProxyModel::clearFilters()
  317. {
  318.   substr.clear();
  319.   invalidateFilter();
  320. }
  321.  
  322. bool ProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
  323. {
  324.   return true;
  325. }
  326.  
  327. void ProxyModel::setSubStr(const QString &new_substr)
  328. {
  329.   if (substr != new_substr)
  330.   {
  331.     substr = new_substr;
  332.     invalidateFilter();
  333.   }
  334. }
  335.  
  336. // ===============================================================================================
  337.  
  338. DetailsDialog::DetailsDialog(QWidget *parent) : QDialog(parent)
  339. {
  340.   QGridLayout *gridLayout = new QGridLayout(this);
  341.  
  342.   QLabel *label_name = new QLabel(this);
  343.   label_name->setText("Name");
  344.   gridLayout->addWidget(label_name, 0, 0, 1, 1);
  345.   lineEdit_name = new QLineEdit(this);
  346.   gridLayout->addWidget(lineEdit_name, 0, 1, 1, 1);
  347.  
  348.   QLabel *label_desc = new QLabel(this);
  349.   label_desc->setText("Description");
  350.   gridLayout->addWidget(label_desc, 1, 0, 1, 1);
  351.  
  352.   textEdit_description = new QTextEdit(this);
  353.   gridLayout->addWidget(textEdit_description, 1, 1, 1, 1);
  354.  
  355.   QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
  356.   buttonBox->setOrientation(Qt::Vertical);
  357.   buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
  358.   gridLayout->addWidget(buttonBox, 0, 2, 2, 1);
  359.  
  360.   QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
  361.   QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
  362. }
  363.  
  364. DetailsDialog::~DetailsDialog() { }
  365.  
  366. void DetailsDialog::setFormValues(DataEntity * const data)
  367. {
  368.   lineEdit_name->setText(data->getName());
  369.   textEdit_description->setText(data->getDescription());
  370. }
  371.  
  372. bool DetailsDialog::getFormValues(DataEntity * const data)
  373. {
  374.   return data->setData(lineEdit_name->text(),
  375.                        textEdit_description->toPlainText());
  376. }
  377.  
  378. // ===============================================================================================
  379.  
  380. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
  381. {
  382.   // Create window widgets
  383.  
  384.   for (uint i=0; i<20; ++i) { DataEntity *de = new DataEntity(); local.append(*de); }
  385.  
  386.   model = new ListModel(0, &local, &global);
  387.  
  388.   proxyModel = new ProxyModel();
  389.   proxyModel->setSourceModel(model);
  390.   proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
  391.   proxyModel->setSortRole(Qt::UserRole);
  392.   proxyModel->setFilterRole(Qt::UserRole);
  393.   proxyModel->sort(1, Qt::AscendingOrder);
  394.   proxyModel->setDynamicSortFilter(true);
  395.  
  396.   QTableView *tableView = new QTableView();
  397.   tableView->setModel(proxyModel);
  398.  
  399.   tableView->setSortingEnabled(true);
  400.   tableView->horizontalHeader()->setStretchLastSection(true);
  401.   //tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
  402.   tableView->horizontalHeader()->setSectionResizeMode(0,QHeaderView::Stretch);
  403.   tableView->horizontalHeader()->hide();
  404.   tableView->verticalHeader()->hide();
  405.   tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  406.   tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
  407.   tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
  408.   tableView->setSelectionMode(QAbstractItemView::SingleSelection);
  409.   tableView->setShowGrid(false);
  410.   tableView->setDragDropMode(QAbstractItemView::DragOnly); // QAbstractItemView::DragDrop
  411.   tableView->setAlternatingRowColors(true);
  412.  
  413.   connect(tableView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(editListEntry(const QModelIndex&)));
  414.  
  415.   QGridLayout *layout = new QGridLayout();
  416.   layout->addWidget(tableView, 0,0,3,1);
  417.  
  418.   QPushButton *addButton = new QPushButton();
  419.   addButton->setText("Add");
  420.   layout->addWidget(addButton, 0,1,1,1);
  421.   connect(addButton, SIGNAL(clicked()), this, SLOT(addListEntry()));
  422.  
  423.   QPushButton *delButton = new QPushButton();
  424.   delButton->setText("Delete");
  425.   layout->addWidget(delButton, 1,1,1,1);
  426.   connect(delButton, SIGNAL(clicked()), this, SLOT(deleteListEntry()));
  427.  
  428.   QWidget* central_widget = new QWidget(this);
  429.   central_widget->setLayout(layout);
  430.   setCentralWidget(central_widget);
  431. }
  432.  
  433. MainWindow::~MainWindow() { }
  434.  
  435. void MainWindow::editListItem(const QModelIndex *index)
  436. {
  437.   DetailsDialog *dialog;
  438.   DataEntity *newItem = 0;
  439.   bool add_item = (index == 0);
  440.  
  441.   qDebug("editListItem: %u %s", (index?index->row():0), (add_item ? "Add New" : "Edit"));
  442.  
  443.   QAbstractItemView *view = this->findChild<QTableView *>();
  444.   if (!view) return;
  445.  
  446.   QModelIndex sourceIndex;
  447.   if (!add_item) sourceIndex = proxyModel->mapToSource(*index);
  448.  
  449.   dialog = new DetailsDialog(this);
  450.   if (add_item)
  451.     newItem = new DataEntity();
  452.   else
  453.   {
  454.     qDebug() << "TABLE NAME=" << model->getItem(*index)->getName();
  455.     qDebug() << "PROXY NAME=" << model->getItem(sourceIndex)->getName();
  456.     dialog->setFormValues(model->getItem(sourceIndex));
  457.   }
  458.  
  459.   if (dialog)
  460.   {
  461.     while (1)
  462.     {
  463.       bool result = false;
  464.  
  465.       // Execute the dialog (and leave loop, if aborted)
  466.       if (dialog->exec() == QDialog::Rejected) break;
  467.  
  468.       // Get values from dialog (and leave loop, if successful)
  469.       if (add_item)
  470.       {
  471.         result = dialog->getFormValues(newItem);
  472.         if (result) local.append(*newItem);
  473.       }
  474.       else
  475.       {
  476.         result = dialog->getFormValues(model->getItem(sourceIndex));
  477.       }
  478.  
  479.       //emit model->dataChanged(*index, *index);
  480.       //emit model->dataChanged(sourceIndex, sourceIndex);
  481.       //emit proxyModel->dataChanged(*index, *index);
  482.  
  483.       // Force resorting of list
  484.       //proxyModel->sort(1, Qt::DescendingOrder);
  485.       //proxyModel->sort(1, Qt::AscendingOrder);
  486.       //model->reset();
  487.       //proxyModel->reset();
  488.       if (result) break;
  489.     }
  490.  
  491.     // Destroy the used dialog
  492.     delete dialog;
  493.   }
  494. }
  495.  
  496. void MainWindow::addListEntry()
  497. {
  498.   editListItem(0);
  499. }
  500.  
  501. void MainWindow::editListEntry(const QModelIndex &index)
  502. {
  503.   qDebug("editListEntry: %u", index.row());
  504.   editListItem(&index);
  505. }
  506.  
  507. void MainWindow::deleteListEntry()
  508. {
  509.   qDebug("deleteListEntry called");
  510.  
  511.   QAbstractItemView *view = this->findChild<QTableView *>();
  512.   if (!view) return;
  513.  
  514.   QModelIndex index = view->currentIndex();
  515.  
  516.   if (index.isValid())
  517.   {
  518.     QModelIndex sourceIndex = proxyModel->mapToSource(index);
  519.  
  520.     qDebug("deleteListEntry: %u", sourceIndex.row());
  521.     model->deleteItem(sourceIndex);
  522.   }
  523. }
  524.  
  525. // ===============================================================================================
  526.  
  527. int main(int argc, char *argv[])
  528. {
  529.   QApplication app(argc, argv);
  530.  
  531.   for (uint i=0; i<20; ++i) { DataEntity *de = new DataEntity(); global.append(*de); }
  532.  
  533.   MainWindow *mainWin = new MainWindow;
  534.   mainWin->resize(800, 640);
  535.   mainWin->show();
  536.   return app.exec();
  537. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement