From 14f9fac873f86c7d17adfb80b3bc13ad378f1829 Mon Sep 17 00:00:00 2001 From: Herwig Birke Date: Mon, 15 Jan 2018 14:34:06 +0100 Subject: [PATCH] . --- cmainwindow.cpp | 47 ++++++++++++ cmainwindow.h | 7 ++ cpartdistributor.h | 2 + cpartlistitemdelegate.cpp | 156 ++++++++++++++++++++++++++++++++++++++ cpartlistitemdelegate.h | 26 +++++++ cpartlistwindow.cpp | 50 ++++++++++++ cpartlistwindow.h | 8 ++ cpartlistwindow.ui | 6 +- qtPartlist.pro | 6 +- 9 files changed, 305 insertions(+), 3 deletions(-) create mode 100644 cpartlistitemdelegate.cpp create mode 100644 cpartlistitemdelegate.h diff --git a/cmainwindow.cpp b/cmainwindow.cpp index ea7b9fa..7222450 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -96,6 +96,10 @@ void cMainWindow::createActions() createSeparator(m_lpMenuPartlist, m_lpToolBarPartlist); createAction(m_lpMenuPartlist, m_lpToolBarPartlist, &m_lpActionPartlistSave, ":/icons/normal/Save file.png", ":/icons/hot/Save file.png", ":/icons/disabled/Save file.png", tr("&save partlist"), QKeySequence::UnknownKey, tr("Save partlist"), &cMainWindow::onMenuPartlistSave); createAction(m_lpMenuPartlist, m_lpToolBarPartlist, &m_lpActionPartlistSaveAs, ":/icons/normal/Save as.png", ":/icons/hot/Save as.png", ":/icons/disabled/Save as.png", tr("save partlist &as..."), QKeySequence::UnknownKey, tr("Save partlist as"), &cMainWindow::onMenuPartlistSaveAs); + createSeparator(m_lpMenuPartlist, m_lpToolBarPartlist); + createAction(m_lpMenuPartlist, m_lpToolBarPartlist, &m_lpActionPartlistPartAdd, ":/icons/normal/Add.png", ":/icons/hot/Add.png", ":/icons/disabled/Add.png", tr("&add part..."), QKeySequence::UnknownKey, tr("Add part"), &cMainWindow::onMenuPartlistPartAdd); + createAction(m_lpMenuPartlist, m_lpToolBarPartlist, &m_lpActionPartlistPartEdit, ":/icons/normal/Edit.png", ":/icons/hot/Edit.png", ":/icons/disabled/Edit.png", tr("&edit part..."), QKeySequence::UnknownKey, tr("Edit part"), &cMainWindow::onMenuPartlistPartEdit); + createAction(m_lpMenuPartlist, m_lpToolBarPartlist, &m_lpActionPartlistPartDelete, ":/icons/normal/Delete.png", ":/icons/hot/Delete.png", ":/icons/disabled/Delete.png", tr("&delete part..."), QKeySequence::UnknownKey, tr("Delete part"), &cMainWindow::onMenuPartlistPartDelete); } cMainWindow::~cMainWindow() @@ -478,6 +482,9 @@ void cMainWindow::updateMenu() m_lpActionPartlistClose->setEnabled(false); m_lpActionPartlistSave->setEnabled(false); m_lpActionPartlistSaveAs->setEnabled(false); + m_lpActionPartlistPartAdd->setEnabled(false); + m_lpActionPartlistPartEdit->setEnabled(false); + m_lpActionPartlistPartDelete->setEnabled(false); return; } @@ -511,6 +518,9 @@ void cMainWindow::updateMenu() m_lpActionPartlistClose->setEnabled(false); m_lpActionPartlistSave->setEnabled(false); m_lpActionPartlistSaveAs->setEnabled(false); + m_lpActionPartlistPartAdd->setEnabled(false); + m_lpActionPartlistPartEdit->setEnabled(false); + m_lpActionPartlistPartDelete->setEnabled(false); } else if(lpPartWindow) { @@ -537,6 +547,9 @@ void cMainWindow::updateMenu() m_lpActionPartlistClose->setEnabled(false); m_lpActionPartlistSave->setEnabled(false); m_lpActionPartlistSaveAs->setEnabled(false); + m_lpActionPartlistPartAdd->setEnabled(false); + m_lpActionPartlistPartEdit->setEnabled(false); + m_lpActionPartlistPartDelete->setEnabled(false); } else if(lpPartlistWindow) { @@ -555,6 +568,18 @@ void cMainWindow::updateMenu() m_lpActionPartsAdd->setEnabled(false); m_lpActionPartsEdit->setEnabled(false); m_lpActionPartsDelete->setEnabled(false); + + m_lpActionPartlistPartAdd->setEnabled(true); + if(lpPartlistWindow->somethingSelected()) + { + m_lpActionPartlistPartEdit->setEnabled(true); + m_lpActionPartlistPartDelete->setEnabled(true); + } + else + { + m_lpActionPartlistPartEdit->setEnabled(false); + m_lpActionPartlistPartDelete->setEnabled(false); + } } } @@ -591,6 +616,11 @@ void cMainWindow::partChanged(cPart* /*lpPart*/) loadPartDistributorList(); } +void cMainWindow::partlistSelectionChanged(const QModelIndex& index) +{ + updateMenu(); +} + void cMainWindow::onMenuPartlistNew() { QList newList; @@ -640,6 +670,7 @@ void cMainWindow::onMenuPartlistNew() lpNew->setList(&m_distributorList, &m_partGroupList, &m_partList, &m_partDistributorList); connect(lpNew, SIGNAL(partlistChanged(QWidget*)), this, SLOT(partlistChanged(QWidget*))); + connect(lpNew, SIGNAL(selectionChanged(QModelIndex)), this, SLOT(partlistSelectionChanged(QModelIndex))); } void cMainWindow::onMenuPartlistOpen() @@ -692,6 +723,7 @@ void cMainWindow::onMenuPartlistOpen() lpNew->setPartlistID(id); connect(lpNew, SIGNAL(partlistChanged(QWidget*)), this, SLOT(partlistChanged(QWidget*))); + connect(lpNew, SIGNAL(selectionChanged(QModelIndex)), this, SLOT(partlistSelectionChanged(QModelIndex))); } } @@ -726,6 +758,21 @@ void cMainWindow::onMenuPartlistSaveAs() lpPartlistWindow->saveAs(); } +void cMainWindow::onMenuPartlistPartAdd() +{ + +} + +void cMainWindow::onMenuPartlistPartEdit() +{ + +} + +void cMainWindow::onMenuPartlistPartDelete() +{ + +} + void cMainWindow::partlistChanged(QWidget* /*lpWidget*/) { updateMenu(); diff --git a/cmainwindow.h b/cmainwindow.h index d101909..a0e8132 100644 --- a/cmainwindow.h +++ b/cmainwindow.h @@ -69,6 +69,9 @@ private: QAction* m_lpActionPartlistClose; QAction* m_lpActionPartlistSave; QAction* m_lpActionPartlistSaveAs; + QAction* m_lpActionPartlistPartAdd; + QAction* m_lpActionPartlistPartEdit; + QAction* m_lpActionPartlistPartDelete; QSqlDatabase m_db; cDistributorList m_distributorList; @@ -147,6 +150,9 @@ private slots: void onMenuPartlistClose(); void onMenuPartlistSave(); void onMenuPartlistSaveAs(); + void onMenuPartlistPartAdd(); + void onMenuPartlistPartEdit(); + void onMenuPartlistPartDelete(); void on_m_lpMainTab_currentChanged(int); @@ -158,6 +164,7 @@ private slots: void partChanged(cPart* lpPart); void partlistChanged(QWidget* lpWidget); + void partlistSelectionChanged(const QModelIndex& index); }; #endif // CMAINWINDOW_H diff --git a/cpartdistributor.h b/cpartdistributor.h index 5de0c08..fdbed0e 100644 --- a/cpartdistributor.h +++ b/cpartdistributor.h @@ -53,4 +53,6 @@ public: cPartDistributor* find(qint32 id); }; +Q_DECLARE_METATYPE(cPartDistributorList*) + #endif // CPARTDISTRIBUTOR_H diff --git a/cpartlistitemdelegate.cpp b/cpartlistitemdelegate.cpp new file mode 100644 index 0000000..8d265d3 --- /dev/null +++ b/cpartlistitemdelegate.cpp @@ -0,0 +1,156 @@ +#include "cpartlistitemdelegate.h" + +#include "cpartdistributor.h" +#include "cpartlistitem.h" + +#include +#include +#include +#include +#include +#include +#include + + +cPartListItemDelegate::cPartListItemDelegate(QObject *parent) : + QStyledItemDelegate(parent) +{ +} + +cPartListItemDelegate::~cPartListItemDelegate() +{ +} + +QWidget* cPartListItemDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const +{ + switch(index.column()) + { + case 0: // reference + { + QTextEdit* lpEdit = new QTextEdit(parent); + return(lpEdit); + } + case 2: // distributor + { + cPartDistributorList* lpList = qvariant_cast(index.data(Qt::UserRole+1)); + QComboBox* lpComboBox = new QComboBox(parent); + cPartlistItem* lpItem = qvariant_cast(index.data(Qt::UserRole)); + + if(!lpItem || !lpList) + return(0); + + for(int x = 0;x < lpList->count();x++) + { + cPartDistributor* lpPartDistributor = lpList->at(x); + + if(lpPartDistributor->part()->id() == lpItem->partID()) + lpComboBox->addItem(lpPartDistributor->distributor()->name(), QVariant::fromValue(lpPartDistributor)); + } + return(lpComboBox); + } + case 4: // price + { + QDoubleSpinBox* lpSpinBox = new QDoubleSpinBox(parent); + lpSpinBox->setMinimum(0); + lpSpinBox->setMaximum(999999); + lpSpinBox->setDecimals(2); + return(lpSpinBox); + } + case 5: // description + return(QStyledItemDelegate::createEditor(parent, option, index)); + default: + return(0); + } + + return(0); +} + +void cPartListItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + switch(index.column()) + { + case 0: // reference + { + QRect rect = option.rect; + rect.setWidth(100); + rect.setHeight(100); + static_cast(editor)->setGeometry(rect); + } + break; + default: + editor->setGeometry(option.rect); + break; + } +} + +void cPartListItemDelegate::setEditorData ( QWidget *editor, const QModelIndex &index ) const +{ + QComboBox* lpComboBox = qobject_cast(editor); + QTextEdit* lpTextEdit = qobject_cast(editor); + + switch(index.column()) + { + case 0: + { + QStringList list = index.data(Qt::EditRole).toString().split(", "); + lpTextEdit->setText(list.join("\n")); + } + break; + case 2: + { + QString currentText = index.data(Qt::EditRole).toString(); + int cbIndex = lpComboBox->findText(currentText); + + if(cbIndex >= 0) + lpComboBox->setCurrentIndex(cbIndex); + } + default: + QStyledItemDelegate::setEditorData(editor, index); + } +} + +void cPartListItemDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const +{ + QComboBox* lpComboBox = qobject_cast(editor); + QDoubleSpinBox* lpSpinBox = qobject_cast(editor); + QTextEdit* lpTextEdit = qobject_cast(editor); + + switch(index.column()) + { + case 0: + { + QStringList list = lpTextEdit->toPlainText().split("\n"); + QStringList list1; + + for(int x = 0;x < list.count();x++) + { + if(!list.at(x).isEmpty()) + list1.append(list.at(x)); + } + model->setData(index, list1.join(", "), Qt::EditRole); + } + break; + case 2: + { + cPartDistributor* lpPartDistributor = qvariant_cast(lpComboBox->currentData()); + + model->setData(index, lpComboBox->currentText(), Qt::EditRole); + + QStandardItemModel* lpModel = (QStandardItemModel*)index.model(); + QStandardItem* lpItem = lpModel->itemFromIndex(lpModel->index(index.row(), 4)); + lpItem->setText(QString::number(lpPartDistributor->price(), 'f', 2)); + } + break; + case 4: + if(lpSpinBox) + { + QStandardItemModel* lpModel = (QStandardItemModel*)index.model(); + QStandardItem* lpItem = lpModel->itemFromIndex(index); + lpItem->setText(QString::number(lpSpinBox->value(), 'f', 2)); + } + break; + default: + QStyledItemDelegate::setModelData(editor, model, index); + break; + } +} diff --git a/cpartlistitemdelegate.h b/cpartlistitemdelegate.h new file mode 100644 index 0000000..c5257fb --- /dev/null +++ b/cpartlistitemdelegate.h @@ -0,0 +1,26 @@ +#ifndef CPARTLISTITEMDELEGATE_H +#define CPARTLISTITEMDELEGATE_H + + +#include +#include + + +class cPartListItemDelegate : public QStyledItemDelegate +{ + Q_OBJECT +public: + cPartListItemDelegate(QObject* parent = 0); + ~cPartListItemDelegate(); + + virtual QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; + virtual void setEditorData(QWidget *editor, const QModelIndex &index) const; + virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; + virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; + +signals: +// void ipRangeChanged(cIPRange* lpIPRange, QStandardItem* lpItem) const; +// void locationChanged(cIPRange* lpIPRange, QStandardItem* lpItem) const; +}; + +#endif // CPARTLISTITEMDELEGATE_H diff --git a/cpartlistwindow.cpp b/cpartlistwindow.cpp index d66eb7b..0723da2 100644 --- a/cpartlistwindow.cpp +++ b/cpartlistwindow.cpp @@ -1,11 +1,15 @@ #include "cpartlistwindow.h" #include "ui_cpartlistwindow.h" +#include "cpartlistitemdelegate.h" + #include "common.h" #include #include +#include + #include #include @@ -25,6 +29,7 @@ cPartlistWindow::cPartlistWindow(QWidget *parent) : m_lpPartListModel = new QStandardItemModel(0, 3); ui->m_lpPartList->setModel(m_lpPartListModel); + ui->m_lpPartList->setItemDelegate(new cPartListItemDelegate()); } cPartlistWindow::~cPartlistWindow() @@ -210,6 +215,7 @@ void cPartlistWindow::showPartList() QStringList header; header << tr("reference") << tr("part") << tr("distributor") << tr("state") << tr("price") << tr("description"); m_lpPartListModel->setHorizontalHeaderLabels(header); + ui->m_lpPartList->header()->setMinimumSectionSize(50); for(int x = 0;x < m_partlistItemList.count();x++) { @@ -227,8 +233,13 @@ void cPartlistWindow::showPartList() lpItems.at(4)->setText(QString::number(lpPartlistItem->price(), 'f', 2)); lpItems.at(5)->setText(lpPartlistItem->description()); + lpItems.at(4)->setTextAlignment(Qt::AlignRight); + for(int z = 0;z < header.count();z++) + { lpItems.at(z)->setData(QVariant::fromValue(lpPartlistItem), Qt::UserRole); + lpItems.at(z)->setData(QVariant::fromValue(m_lpPartDistributorList), Qt::UserRole+1); + } m_lpPartListModel->appendRow(lpItems); } @@ -267,6 +278,45 @@ bool cPartlistWindow::somethingChanged() return(m_bSomethingChanged); } +bool cPartlistWindow::somethingSelected() +{ + if(ui->m_lpPartList->selectionModel()->selectedRows().count()) + return(true); + return(false); +} + void cPartlistWindow::on_m_lpPartList_doubleClicked(const QModelIndex &/*index*/) { } + +void cPartlistWindow::on_m_lpPartList_clicked(const QModelIndex &index) +{ + selectionChanged(index); +} + +void cPartlistWindow::on_m_lpPartList_customContextMenuRequested(const QPoint &pos) +{ + QMenu* lpMenu = new QMenu(this); + + lpMenu->addAction("add", this, SLOT(onPartAdd())); + + if(somethingSelected()) + { + lpMenu->addAction("edit", this, SLOT(onPartEdit())); + lpMenu->addAction("delete", this, SLOT(onPartDelete())); + } + + lpMenu->popup(ui->m_lpPartList->viewport()->mapToGlobal(pos)); +} + +void cPartlistWindow::onPartAdd() +{ +} + +void cPartlistWindow::onPartEdit() +{ +} + +void cPartlistWindow::onPartDelete() +{ +} diff --git a/cpartlistwindow.h b/cpartlistwindow.h index b9befb4..3e0caf3 100644 --- a/cpartlistwindow.h +++ b/cpartlistwindow.h @@ -39,12 +39,20 @@ public: bool saveAs(); bool somethingChanged(); + bool somethingSelected(); private slots: void on_m_lpName_textChanged(const QString &arg1); void on_m_lpPartList_doubleClicked(const QModelIndex &index); + void on_m_lpPartList_clicked(const QModelIndex &index); + void on_m_lpPartList_customContextMenuRequested(const QPoint &pos); + + void onPartAdd(); + void onPartEdit(); + void onPartDelete(); signals: void partlistChanged(QWidget* lpWidget) const; + void selectionChanged(const QModelIndex& index) const; private: Ui::cPartlistWindow* ui; diff --git a/cpartlistwindow.ui b/cpartlistwindow.ui index 502db6a..264a24f 100644 --- a/cpartlistwindow.ui +++ b/cpartlistwindow.ui @@ -93,7 +93,11 @@ - + + + Qt::CustomContextMenu + + diff --git a/qtPartlist.pro b/qtPartlist.pro index 1ebec70..04f2ca6 100644 --- a/qtPartlist.pro +++ b/qtPartlist.pro @@ -38,7 +38,8 @@ SOURCES += \ cpartdistributor.cpp \ clabel.cpp \ cpartdistributoreditdialog.cpp \ - cpartlistitem.cpp + cpartlistitem.cpp \ + cpartlistitemdelegate.cpp HEADERS += \ cmainwindow.h \ @@ -54,7 +55,8 @@ HEADERS += \ cpartdistributor.h \ clabel.h \ cpartdistributoreditdialog.h \ - cpartlistitem.h + cpartlistitem.h \ + cpartlistitemdelegate.h FORMS += \ cmainwindow.ui \