diff --git a/cdistributoreditdialog.h b/cdistributoreditdialog.h index be8ad8a..1482def 100644 --- a/cdistributoreditdialog.h +++ b/cdistributoreditdialog.h @@ -19,20 +19,19 @@ public: explicit cDistributorEditDialog(QWidget *parent = 0); ~cDistributorEditDialog(); - qint32 id(); - - void setValues(cDistributor* lpDistributor); + qint32 id(); + void setValues(cDistributor* lpDistributor); private slots: - void on_m_lpName_textChanged(const QString &arg1); + void on_m_lpName_textChanged(const QString &arg1); private: - Ui::cDistributorEditDialog *ui; - qint32 m_id; + Ui::cDistributorEditDialog* ui; + qint32 m_id; - bool save(); - bool add(); + bool save(); + bool add(); protected: - void accept(); + void accept(); }; #endif // CDISTRIBUTOREDITDIALOG_H diff --git a/cdistributoreditdialog.ui b/cdistributoreditdialog.ui index 2d21857..0b50294 100644 --- a/cdistributoreditdialog.ui +++ b/cdistributoreditdialog.ui @@ -6,8 +6,8 @@ 0 0 - 515 - 347 + 500 + 350 diff --git a/cdistributorwindow.cpp b/cdistributorwindow.cpp index c21bbcc..a97f9b4 100644 --- a/cdistributorwindow.cpp +++ b/cdistributorwindow.cpp @@ -3,7 +3,6 @@ #include "cdistributoreditdialog.h" - #include #include #include @@ -67,6 +66,9 @@ void cDistributorWindow::showDistributorList() m_lpDistributorListModel->appendRow(lpItems); } + + for(int z = 0;z < header.count();z++) + ui->m_lpDistributorList->resizeColumnToContents(z); } bool cDistributorWindow::somethingSelected() diff --git a/cmainwindow.cpp b/cmainwindow.cpp index 9960c4e..42b6bef 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -1,14 +1,20 @@ #include "cmainwindow.h" #include "ui_cmainwindow.h" +#include "cpartlistwindow.h" + #include "common.h" #include #include +#include + #include #include +#include + #include @@ -34,6 +40,8 @@ cMainWindow::cMainWindow(QWidget *parent) : initDB(); loadDistributorList(); + loadPartGroupList(); + loadPartList(); updateMenu(); } @@ -76,12 +84,22 @@ void cMainWindow::initDB() QSqlQuery query; + if(!m_db.tables().contains("partgroup")) + { + query.exec("CREATE TABLE partgroup (" + " id INTEGER PRIMARY KEY," + " name STRING NOT NULL," + " description TEXT);"); + } + if(!m_db.tables().contains("part")) { query.exec("CREATE TABLE part (" " id INTEGER PRIMARY KEY," " name STRING NOT NULL," - " description TEXT);"); + " partgroupID INTEGER REFERENCES partgroup(id)," + " description TEXT," + " link STRING);"); } if(!m_db.tables().contains("distributor")) @@ -99,6 +117,18 @@ void cMainWindow::initDB() " description TEXT);"); } + if(!m_db.tables().contains("part_distributor")) + { + query.exec("CREATE TABLE part_distributor (" + " id INTEGER PRIMARY KEY," + " name STRING," + " description TEXT," + " partID INTEGER REFERENCES part (id)," + " distributorID INTEGER REFERENCES distributor (id)," + " price DOUBLE," + " link STRING);"); + } + if(!m_db.tables().contains("partlist")) { query.exec("CREATE TABLE partlist (" @@ -110,21 +140,23 @@ void cMainWindow::initDB() if(!m_db.tables().contains("partlistitem")) { query.exec("CREATE TABLE partlistitem (" - " id INTEGER PRIMARY KEY," - " partlistID INTEGER REFERENCES partlist (id)," - " partID INTEGER REFERENCES part (id)," - " reference STRING," - " description TEXT," - " distributorID INTEGER REFERENCES distributor (id)," - " ordered BOOLEAN," - " link STRING);"); + " id INTEGER PRIMARY KEY," + " partlistID INTEGER REFERENCES partlist (id)," + " part_disttributorID INTEGER REFERENCES part_distributor (id)," + " replaceID INTEGER REFERENCES partlistitem (id)," + " reference STRING," + " description TEXT," + " state INTEGER," + " price DOUBLE);"); } if(!m_db.tables().contains("project")) { query.exec("CREATE TABLE project (" " id INTEGER PRIMARY KEY," - " name STRING);"); + " name STRING," + " initialDate DATE," + " description);"); } if(!m_db.tables().contains("project_partlist")) @@ -170,6 +202,64 @@ void cMainWindow::loadDistributorList() } } +void cMainWindow::loadPartGroupList() +{ + m_partGroupList.clear(); + + QSqlQuery query; + QString szQuery; + + szQuery = "SELECT id, name, description FROM partgroup ORDER BY name;"; + + if(!query.exec(szQuery)) + { + qDebug() << query.lastError().text(); + return; + } + + while(query.next()) + { + cPartGroup* lpPartGroup = m_partGroupList.add(query.value("id").toInt()); + if(!lpPartGroup) + return; + + lpPartGroup->setName(query.value("name").toString()); + lpPartGroup->setDescription(query.value("description").toString()); + } +} + +void cMainWindow::loadPartList() +{ + m_partList.clear(); + + QSqlQuery query; + QString szQuery; + + szQuery = "SELECT part.id id, part.name name, part.description description, part.partgroupID partgroupID, part.link FROM part JOIN partgroup ON (part.partgroupID = partgroup.id) ORDER BY partgroup.name, part.name;"; + + if(!query.exec(szQuery)) + { + qDebug() << query.lastError().text(); + return; + } + + while(query.next()) + { + cPartGroup* lpPartGroup = m_partGroupList.find(query.value("part.partgroupID").toInt()); + if(!lpPartGroup) + return; + + cPart* lpPart = m_partList.add(query.value("part.id").toInt()); + if(!lpPart) + return; + + lpPart->setName(query.value("part.name").toString()); + lpPart->setDescription(query.value("part.description").toString()); + lpPart->setPartGroup(lpPartGroup); + lpPart->setLink(query.value("part.link").toString()); + } +} + void cMainWindow::on_m_lpMenuFileNewProject_triggered() { @@ -239,23 +329,36 @@ void cMainWindow::on_m_lpMenuPartsShow_triggered() { m_lpPartWindow = new cPartWindow(this); ui->m_lpMainTab->addTab(m_lpPartWindow, tr("Parts")); + m_lpPartWindow->setList(&m_partGroupList, &m_partList); + + connect(m_lpPartWindow, SIGNAL(selectionChanged(QModelIndex)), this, SLOT(partSelectionChanged(QModelIndex))); + connect(m_lpPartWindow, SIGNAL(partChanged(cPart*)), this, SLOT(partChanged(cPart*))); } ui->m_lpMainTab->setCurrentWidget(m_lpPartWindow); } void cMainWindow::on_m_lpMenuPartsAdd_triggered() { + if(!m_lpPartWindow) + return; + m_lpPartWindow->addPart(); } void cMainWindow::on_m_lpMenuPartsEdit_triggered() { + if(!m_lpPartWindow) + return; + m_lpPartWindow->editPart(); } void cMainWindow::on_m_lpMenuPartsDelete_triggered() { + if(!m_lpPartWindow) + return; + m_lpPartWindow->deletePart(); } void cMainWindow::updateMenu() @@ -268,6 +371,17 @@ void cMainWindow::updateMenu() ui->m_lpMenuDistributorEdit->setEnabled(false); ui->m_lpMenuDistributorDelete->setEnabled(false); + ui->m_lpMenuPartsShow->setEnabled(true); + ui->m_lpMenuPartsAdd->setEnabled(false); + ui->m_lpMenuPartsEdit->setEnabled(false); + ui->m_lpMenuPartsDelete->setEnabled(false); + + ui->m_lpMenuPartlistNew->setEnabled(true); + ui->m_lpMenuPartlistOpen->setEnabled(true); + ui->m_lpMenuPartlistClose->setEnabled(false); + ui->m_lpMenuPartlistSave->setEnabled(false); + ui->m_lpMenuPartlistSaveAs->setEnabled(false); + return; } @@ -289,13 +403,61 @@ void cMainWindow::updateMenu() ui->m_lpMenuDistributorEdit->setEnabled(false); ui->m_lpMenuDistributorDelete->setEnabled(false); } + + ui->m_lpMenuPartsShow->setEnabled(true); + ui->m_lpMenuPartsAdd->setEnabled(false); + ui->m_lpMenuPartsEdit->setEnabled(false); + ui->m_lpMenuPartsDelete->setEnabled(false); + + ui->m_lpMenuPartlistNew->setEnabled(true); + ui->m_lpMenuPartlistOpen->setEnabled(true); + ui->m_lpMenuPartlistClose->setEnabled(false); + ui->m_lpMenuPartlistSave->setEnabled(false); + ui->m_lpMenuPartlistSaveAs->setEnabled(false); } - else + else if(lpPartWindow) { + ui->m_lpMenuPartsShow->setEnabled(false); + ui->m_lpMenuPartsAdd->setEnabled(true); + if(!lpPartWindow->groupSelected()) + { + ui->m_lpMenuPartsEdit->setEnabled(true); + ui->m_lpMenuPartsDelete->setEnabled(true); + } + else + { + ui->m_lpMenuPartsEdit->setEnabled(false); + ui->m_lpMenuPartsDelete->setEnabled(false); + } + ui->m_lpMenuDistributorShow->setEnabled(true); ui->m_lpMenuDistributorAdd->setEnabled(false); ui->m_lpMenuDistributorEdit->setEnabled(false); ui->m_lpMenuDistributorDelete->setEnabled(false); + + ui->m_lpMenuPartlistNew->setEnabled(true); + ui->m_lpMenuPartlistOpen->setEnabled(true); + ui->m_lpMenuPartlistClose->setEnabled(false); + ui->m_lpMenuPartlistSave->setEnabled(false); + ui->m_lpMenuPartlistSaveAs->setEnabled(false); + } + else if(lpPartlistWindow) + { + ui->m_lpMenuPartlistNew->setEnabled(true); + ui->m_lpMenuPartlistOpen->setEnabled(true); + ui->m_lpMenuPartlistClose->setEnabled(true); + ui->m_lpMenuPartlistSave->setEnabled(lpPartlistWindow->somethingChanged()); + ui->m_lpMenuPartlistSaveAs->setEnabled(true); + + ui->m_lpMenuDistributorShow->setEnabled(true); + ui->m_lpMenuDistributorAdd->setEnabled(false); + ui->m_lpMenuDistributorEdit->setEnabled(false); + ui->m_lpMenuDistributorDelete->setEnabled(false); + + ui->m_lpMenuPartsShow->setEnabled(true); + ui->m_lpMenuPartsAdd->setEnabled(false); + ui->m_lpMenuPartsEdit->setEnabled(false); + ui->m_lpMenuPartsDelete->setEnabled(false); } } @@ -304,12 +466,164 @@ void cMainWindow::on_m_lpMainTab_currentChanged(int /*index*/) updateMenu(); } -void cMainWindow::distributorSelectionChanged(const QModelIndex& index) +void cMainWindow::distributorSelectionChanged(const QModelIndex& /*index*/) { updateMenu(); } -void cMainWindow::distributorChanged(cDistributor* lpDistributor) +void cMainWindow::distributorChanged(cDistributor* /*lpDistributor*/) { loadDistributorList(); } + +void cMainWindow::partSelectionChanged(const QModelIndex& /*index*/) +{ + updateMenu(); +} + +void cMainWindow::partGroupChanged(cPartGroup* /*lpPartGroup*/) +{ + loadPartGroupList(); + loadPartList(); +} + +void cMainWindow::partChanged(cPart* /*lpPart*/) +{ + loadPartList(); +} + +void cMainWindow::on_m_lpMenuPartlistNew_triggered() +{ + QList newList; + + for(int x = 0;x < ui->m_lpMainTab->count();x++) + { + cPartlistWindow* lpPartlistWindow = qobject_cast(ui->m_lpMainTab->widget(x)); + + if(!lpPartlistWindow) + continue; + + QString szTitle = lpPartlistWindow->windowTitle(); + if(!szTitle.startsWith("new")) + continue; + + if(szTitle.length() == 3) + { + newList.append(0); + continue; + } + + if(!szTitle.contains("(")) + continue; + + szTitle = szTitle.mid(szTitle.indexOf("(")+1); + if(!szTitle.contains(")")) + continue; + + szTitle = szTitle.left(szTitle.indexOf(")")); + newList.append(szTitle.toInt()); + } + + std::sort(newList.begin(), newList.end()); + QString szNew; + + if(newList.isEmpty()) + szNew = "new"; + else + szNew = QString("new (%1)").arg(newList.last()+1); + + cPartlistWindow* lpNew = new cPartlistWindow(ui->m_lpMainTab); + lpNew->setPartlistName(szNew); + lpNew->setMainTab(ui->m_lpMainTab); + ui->m_lpMainTab->addTab(lpNew, szNew); + ui->m_lpMainTab->setCurrentWidget(lpNew); + + connect(lpNew, SIGNAL(partlistChanged(QWidget*)), this, SLOT(partlistChanged(QWidget*))); +} + +void cMainWindow::on_m_lpMenuPartlistOpen_triggered() +{ + QStringList szList; + QSqlQuery query; + QList idList; + + if(!query.exec("SELECT id, name, description FROM partlist ORDER BY name;")) + { + qDebug() << query.lastError().text(); + QMessageBox::critical(this, "Error", "No projects found."); + return; + } + + while(query.next()) + { + QString sz = query.value("name").toString(); + if(!query.value("description").toString().isEmpty()) + sz.append(QString(" (%1)").arg(query.value("description").toString())); + + szList.append(sz); + idList.append(query.value("id").toInt()); + } + + if(!szList.count()) + { + QMessageBox::critical(this, "Error", "No projects found."); + return; + } + + QInputDialog inputDialog(this); + inputDialog.setOptions(QInputDialog::UseListViewForComboBoxItems); + inputDialog.setComboBoxItems(szList); + inputDialog.setLabelText("Project:"); + inputDialog.setWindowTitle("Open"); + + if(inputDialog.exec() == QDialog::Accepted) + { + QString szResult = inputDialog.textValue(); + qint16 index = szList.indexOf(szResult); + qint32 id = idList.at(index); + + cPartlistWindow* lpNew = new cPartlistWindow(ui->m_lpMainTab); + lpNew->setMainTab(ui->m_lpMainTab); + ui->m_lpMainTab->addTab(lpNew, "INITIALIZING"); + ui->m_lpMainTab->setCurrentWidget(lpNew); + lpNew->setPartlistID(id); + + connect(lpNew, SIGNAL(partlistChanged(QWidget*)), this, SLOT(partlistChanged(QWidget*))); + } +} + +void cMainWindow::on_m_lpMenuPartlistClose_triggered() +{ + cPartlistWindow* lpPartlistWindow = qobject_cast(ui->m_lpMainTab->widget(ui->m_lpMainTab->currentIndex())); + + if(!lpPartlistWindow) + return; + + if(lpPartlistWindow->close()) + ui->m_lpMainTab->removeTab(ui->m_lpMainTab->currentIndex()); +} + +void cMainWindow::on_m_lpMenuPartlistSave_triggered() +{ + cPartlistWindow* lpPartlistWindow = qobject_cast(ui->m_lpMainTab->widget(ui->m_lpMainTab->currentIndex())); + + if(!lpPartlistWindow) + return; + + lpPartlistWindow->save(); +} + +void cMainWindow::on_m_lpMenuPartlistSaveAs_triggered() +{ + cPartlistWindow* lpPartlistWindow = qobject_cast(ui->m_lpMainTab->widget(ui->m_lpMainTab->currentIndex())); + + if(!lpPartlistWindow) + return; + + lpPartlistWindow->saveAs(); +} + +void cMainWindow::partlistChanged(QWidget* /*lpWidget*/) +{ + updateMenu(); +} diff --git a/cmainwindow.h b/cmainwindow.h index eef2ecc..9360c81 100644 --- a/cmainwindow.h +++ b/cmainwindow.h @@ -7,6 +7,8 @@ #include "cpartwindow.h" #include "cdistributor.h" +#include "cpartgroup.h" +#include "cpart.h" #include #include @@ -30,12 +32,16 @@ private: Ui::cMainWindow* ui; QSqlDatabase m_db; cDistributorList m_distributorList; + cPartGroupList m_partGroupList; + cPartList m_partList; cDistributorWindow* m_lpDistributorWindow; cPartWindow* m_lpPartWindow; void initDB(); void loadDistributorList(); + void loadPartGroupList(); + void loadPartList(); void updateMenu(); protected: @@ -46,10 +52,12 @@ private slots: void on_m_lpMenuFileCloseProject_triggered(); void on_m_lpMenuFileExport_triggered(); void on_m_lpMenuFileClose_triggered(); + void on_m_lpMenuDistributorShow_triggered(); void on_m_lpMenuDistributorAdd_triggered(); void on_m_lpMenuDistributorEdit_triggered(); void on_m_lpMenuDistributorDelete_triggered(); + void on_m_lpMenuPartsShow_triggered(); void on_m_lpMenuPartsAdd_triggered(); void on_m_lpMenuPartsEdit_triggered(); @@ -58,6 +66,18 @@ private slots: void distributorSelectionChanged(const QModelIndex& index); void distributorChanged(cDistributor* lpDistributor); + + void partSelectionChanged(const QModelIndex& index); + void partGroupChanged(cPartGroup* lpPartGroup); + void partChanged(cPart* lpPart); + + void partlistChanged(QWidget* lpWidget); + + void on_m_lpMenuPartlistNew_triggered(); + void on_m_lpMenuPartlistOpen_triggered(); + void on_m_lpMenuPartlistClose_triggered(); + void on_m_lpMenuPartlistSave_triggered(); + void on_m_lpMenuPartlistSaveAs_triggered(); }; #endif // CMAINWINDOW_H diff --git a/cmainwindow.ui b/cmainwindow.ui index fef8dcb..33bd6c4 100644 --- a/cmainwindow.ui +++ b/cmainwindow.ui @@ -20,6 +20,9 @@ -1 + + true + @@ -64,9 +67,22 @@ + + + Part&list + + + + + + + + + + @@ -150,6 +166,31 @@ delete + + + &new... + + + + + &open... + + + + + &close + + + + + &save + + + + + save &as... + + diff --git a/cpart.cpp b/cpart.cpp new file mode 100644 index 0000000..df60a7b --- /dev/null +++ b/cpart.cpp @@ -0,0 +1,76 @@ +#include "cpart.h" + + +cPart::cPart() : + m_id(-1), + m_szName(""), + m_szDescription(""), + m_szLink(""), + m_lpPartGroup(0) +{ + +} + +void cPart::setID(const qint32& id) +{ + m_id = id; +} + +qint32 cPart::id() +{ + return(m_id); +} + +void cPart::setName(const QString& szName) +{ + m_szName = szName; +} + +QString cPart::name() +{ + return(m_szName); +} + +void cPart::setDescription(const QString& szDescription) +{ + m_szDescription = szDescription; +} + +QString cPart::description() +{ + return(m_szDescription); +} + +void cPart::setLink(const QString& szLink) +{ + m_szLink = szLink; +} + +QString cPart::link() +{ + return(m_szLink); +} + +void cPart::setPartGroup(cPartGroup* lpPartGroup) +{ + m_lpPartGroup = lpPartGroup; +} + +cPartGroup* cPart::partGroup() +{ + return(m_lpPartGroup); +} + +cPart* cPartList::add(qint32 id) +{ + for(int x = 0;x < count();x++) + { + if(at(x)->id() == id) + return(at(x)); + } + + cPart* lpPart = new cPart; + lpPart->setID(id); + append(lpPart); + return(lpPart); +} diff --git a/cpart.h b/cpart.h new file mode 100644 index 0000000..502f979 --- /dev/null +++ b/cpart.h @@ -0,0 +1,46 @@ +#ifndef CPART_H +#define CPART_H + + +#include "cpartgroup.h" + +#include +#include + + +class cPart +{ +public: + cPart(); + + void setID(const qint32& id); + qint32 id(); + + void setName(const QString& szName); + QString name(); + + void setDescription(const QString& szDescription); + QString description(); + + void setLink(const QString& szLink); + QString link(); + + void setPartGroup(cPartGroup* lpPartGroup); + cPartGroup* partGroup(); +private: + qint32 m_id; + QString m_szName; + QString m_szDescription; + QString m_szLink; + cPartGroup* m_lpPartGroup; +}; + +Q_DECLARE_METATYPE(cPart*) + +class cPartList : public QList +{ +public: + cPart* add(qint32 id); +}; + +#endif // CPART_H diff --git a/cparteditdialog.cpp b/cparteditdialog.cpp new file mode 100644 index 0000000..ee93ec0 --- /dev/null +++ b/cparteditdialog.cpp @@ -0,0 +1,215 @@ +#include "cparteditdialog.h" +#include "ui_cparteditdialog.h" + + +#include + +#include +#include + +#include +#include + +#include + + +cPartEditDialog::cPartEditDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::cPartEditDialog), + m_id(-1), + m_lpPart(0), + m_lpPartGroupList(0) +{ + ui->setupUi(this); + ui->m_lpButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false); +} + +cPartEditDialog::~cPartEditDialog() +{ + delete ui; +} + +void cPartEditDialog::setValues(cPart* lpPart, cPartGroupList* lpPartGroupList) +{ + m_lpPart = lpPart; + m_lpPartGroupList = lpPartGroupList; + + for(int x = 0;x < m_lpPartGroupList->count();x++) + ui->m_lpGroup->addItem(m_lpPartGroupList->at(x)->name(), QVariant::fromValue(m_lpPartGroupList->at(x))); + + if(lpPart) + { + ui->m_lpName->setText(lpPart->name()); + ui->m_lpLink->setText(lpPart->link()); + ui->m_lpDescription->setText(lpPart->description()); + + ui->m_lpGroup->setCurrentText(lpPart->partGroup()->name()); + + m_id = lpPart->id(); + } +} + +void cPartEditDialog::on_m_lpName_textChanged(const QString &arg1) +{ + if(arg1.isEmpty()) + ui->m_lpButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false); + else + ui->m_lpButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true); +} + +void cPartEditDialog::accept() +{ + if(m_id != -1) + { + if(save()) + QDialog::accept(); + } + else + { + if(add()) + QDialog::accept(); + } +} + +bool cPartEditDialog::save() +{ + QSqlQuery query; + QString szQuery; + + szQuery = QString("SELECT name FROM part WHERE name=:name AND id <> :id AND partgroupID = :partgroupID;"); + + query.prepare(szQuery); + query.bindValue(":name", ui->m_lpName->text()); + query.bindValue(":id", m_id); + query.bindValue(":partgroupID", qvariant_cast(ui->m_lpGroup->currentData(Qt::UserRole))->id()); + if(!query.exec()) + { + qDebug() << query.lastError().text(); + return(false); + } + + if(query.next()) + { + QMessageBox::critical(this, "ERROR", "Part name already exists."); + return(false); + } + + szQuery = QString("UPDATE part SET name=:name, link=:link, description=:description, partgroupID = :partgroupID WHERE id=:id;"); + query.prepare(szQuery); + query.bindValue(":name", ui->m_lpName->text()); + query.bindValue(":link", ui->m_lpLink->text()); + query.bindValue(":description", ui->m_lpDescription->document()->toPlainText()); + query.bindValue(":id", m_id); + query.bindValue(":partgroupID", qvariant_cast(ui->m_lpGroup->currentData(Qt::UserRole))->id()); + + if(!query.exec()) + { + qDebug() << query.lastError().text(); + return(false); + } + return(true); +} + +bool cPartEditDialog::add() +{ + QSqlQuery query; + QString szQuery; + + szQuery = QString("SELECT name FROM part WHERE name=:name AND partgroupID = :partgroupID;"); + query.prepare(szQuery); + query.bindValue(":name", ui->m_lpName->text()); + query.bindValue(":partgroupID", qvariant_cast(ui->m_lpGroup->currentData(Qt::UserRole))->id()); + if(!query.exec()) + { + qDebug() << query.lastError().text(); + return(false); + } + + if(query.next()) + { + QMessageBox::critical(this, "ERROR", "Part already exists."); + return(false); + } + + szQuery = QString("INSERT INTO part (name, link, description, partgroupID) VALUES (:name, :link, :description, :partgroupID);"); + query.prepare(szQuery); + query.bindValue(":name", ui->m_lpName->text()); + query.bindValue(":link", ui->m_lpLink->text()); + query.bindValue(":description", ui->m_lpDescription->document()->toPlainText()); + query.bindValue(":partgroupID", qvariant_cast(ui->m_lpGroup->currentData(Qt::UserRole))->id()); + + if(!query.exec()) + { + qDebug() << query.lastError().text(); + return(false); + } + + szQuery = QString("SELECT name FROM part WHERE name=:name AND partgroupID = :partgroupID;"); + query.prepare(szQuery); + query.bindValue(":name", ui->m_lpName->text()); + query.bindValue(":partgroupID", qvariant_cast(ui->m_lpGroup->currentData(Qt::UserRole))->id()); + if(!query.exec()) + { + qDebug() << query.lastError().text(); + return(false); + } + + if(!query.next()) + { + qDebug() << query.lastError().text(); + return(false); + } + + m_id = query.value("id").toInt(); + + return(true); +} + +qint32 cPartEditDialog::id() +{ + return(m_id); +} + +void cPartEditDialog::on_m_lpGroupAdd_clicked() +{ + bool bOK; + QString szGroup = QInputDialog::getText(this, tr("Part Group"), tr("Part Group:"), QLineEdit::Normal, "", &bOK); + + if(bOK && !szGroup.isEmpty()) + { + QSqlQuery query; + + query.prepare("SELECT id FROM partgroup WHERE name=:name;"); + query.bindValue(":name", szGroup); + if(!query.exec()) + qDebug() << query.lastError().text(); + else + { + if(query.next()) + QMessageBox::information(this, "Group", "Group already exists."); + else + { + query.prepare("INSERT INTO partgroup (name) VALUES(:name);"); + query.bindValue(":name", szGroup); + if(!query.exec()) + qDebug() << query.lastError().text(); + else + { + query.prepare("SELECT id FROM partgroup WHERE name=:name;"); + query.bindValue(":name", szGroup); + if(!query.exec()) + qDebug() << query.lastError().text(); + else + { + cPartGroup* lpGroup = m_lpPartGroupList->add(query.value("id").toInt()); + lpGroup->setName(szGroup); + + setValues(m_lpPart, m_lpPartGroupList); + + ui->m_lpGroup->setCurrentText(szGroup); + } + } + } + } + } +} diff --git a/cparteditdialog.h b/cparteditdialog.h new file mode 100644 index 0000000..08b78ab --- /dev/null +++ b/cparteditdialog.h @@ -0,0 +1,40 @@ +#ifndef CPARTEDITDIALOG_H +#define CPARTEDITDIALOG_H + + +#include "cpart.h" + +#include + + +namespace Ui { +class cPartEditDialog; +} + +class cPartEditDialog : public QDialog +{ + Q_OBJECT + +public: + explicit cPartEditDialog(QWidget *parent = 0); + ~cPartEditDialog(); + + qint32 id(); + void setValues(cPart* lpPart, cPartGroupList* lpPartGroupList); +private slots: + void on_m_lpName_textChanged(const QString &arg1); + void on_m_lpGroupAdd_clicked(); + +private: + Ui::cPartEditDialog* ui; + qint32 m_id; + cPart* m_lpPart; + cPartGroupList* m_lpPartGroupList; + + bool save(); + bool add(); +protected: + void accept(); +}; + +#endif // CPARTEDITDIALOG_H diff --git a/cparteditdialog.ui b/cparteditdialog.ui new file mode 100644 index 0000000..3f84a32 --- /dev/null +++ b/cparteditdialog.ui @@ -0,0 +1,131 @@ + + + cPartEditDialog + + + + 0 + 0 + 500 + 300 + + + + Part + + + + + + + + + + Name: + + + + + + + + + + + + + + Group: + + + + + + + + + + add... + + + + + + + + + + + Web: + + + + + + + + + + + + + + Description: + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + + + m_lpButtonBox + accepted() + cPartEditDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + m_lpButtonBox + rejected() + cPartEditDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/cpartgroup.cpp b/cpartgroup.cpp new file mode 100644 index 0000000..0db7221 --- /dev/null +++ b/cpartgroup.cpp @@ -0,0 +1,62 @@ +#include "cpartgroup.h" + + +cPartGroup::cPartGroup() : + m_id(-1), + m_szName(""), + m_szDescription("") +{ + +} + +void cPartGroup::setID(const qint32& id) +{ + m_id = id; +} + +qint32 cPartGroup::id() +{ + return(m_id); +} + +void cPartGroup::setName(const QString& szName) +{ + m_szName = szName; +} + +QString cPartGroup::name() +{ + return(m_szName); +} + +void cPartGroup::setDescription(const QString& szDescription) +{ + m_szDescription = szDescription; +} + +QString cPartGroup::description() +{ + return(m_szDescription); +} + +cPartGroup* cPartGroupList::add(qint32 id) +{ + cPartGroup* lpPartGroup = find(id); + if(lpPartGroup) + return(lpPartGroup); + + lpPartGroup = new cPartGroup; + lpPartGroup->setID(id); + append(lpPartGroup); + return(lpPartGroup); +} + +cPartGroup* cPartGroupList::find(qint32 id) +{ + for(int x = 0;x < count();x++) + { + if(at(x)->id() == id) + return(at(x)); + } + return(0); +} diff --git a/cpartgroup.h b/cpartgroup.h new file mode 100644 index 0000000..9303cea --- /dev/null +++ b/cpartgroup.h @@ -0,0 +1,37 @@ +#ifndef CPARTGROUP_H +#define CPARTGROUP_H + + +#include +#include + + +class cPartGroup +{ +public: + cPartGroup(); + + void setID(const qint32& id); + qint32 id(); + + void setName(const QString& szName); + QString name(); + + void setDescription(const QString& szDescription); + QString description(); +private: + qint32 m_id; + QString m_szName; + QString m_szDescription; +}; + +Q_DECLARE_METATYPE(cPartGroup*) + +class cPartGroupList : public QList +{ +public: + cPartGroup* add(qint32 id); + cPartGroup* find(qint32 id); +}; + +#endif // CPARTGROUP_H diff --git a/cpartlistwindow.cpp b/cpartlistwindow.cpp index a67bfcf..b9aa051 100644 --- a/cpartlistwindow.cpp +++ b/cpartlistwindow.cpp @@ -2,15 +2,193 @@ #include "ui_cpartlistwindow.h" +#include +#include + +#include +#include + + cPartlistWindow::cPartlistWindow(QWidget *parent) : QWidget(parent), - ui(new Ui::cPartlistWindow) + ui(new Ui::cPartlistWindow), + m_lpMainTab(0), + m_id(-1), + m_bSomethingChanged(false) { ui->setupUi(this); } - cPartlistWindow::~cPartlistWindow() { delete ui; } + +void cPartlistWindow::setMainTab(QTabWidget *lpMainTab) +{ + m_lpMainTab = lpMainTab; +} + +void cPartlistWindow::setPartlistName(const QString& szPartlistName) +{ + setWindowTitle(szPartlistName); + ui->m_lpName->setText(szPartlistName); + m_bSomethingChanged = true; +} + +QString cPartlistWindow::partlistName() +{ + return(ui->m_lpName->text()); +} + +void cPartlistWindow::setPartlistID(const qint32& id) +{ + m_id = id; + + QSqlQuery query; + + query.prepare("SELECT name, description FROM partlist WHERE id=:id;"); + query.bindValue(":id", m_id); + + if(!query.exec()) + { + qDebug() << query.lastError().text(); + return; + } + + if(!query.next()) + { + qDebug() << query.lastError().text(); + return; + } + + ui->m_lpName->setText(query.value("name").toString()); + ui->m_lpDescription->setText(query.value("description").toString()); + + if(m_lpMainTab) + { + m_lpMainTab->setTabText(m_lpMainTab->currentIndex(), query.value("name").toString()); + partlistChanged(this); + } +} + +void cPartlistWindow::on_m_lpName_textChanged(const QString &arg1) +{ + if(m_lpMainTab) + { + m_bSomethingChanged = true; + m_lpMainTab->setTabText(m_lpMainTab->currentIndex(), arg1); + partlistChanged(this); + } +} + +bool cPartlistWindow::close() +{ + qint32 ret = QMessageBox::question(this, tr("Close"), tr("Do you want to save changes?"), QMessageBox::Yes, QMessageBox::No, QMessageBox::Abort); + + switch(ret) + { + case QMessageBox::Yes: + return(save()); + break; + case QMessageBox::No: + return(false); + break; + case QMessageBox::Abort: + return(false); + } + return(false); +} + +bool cPartlistWindow::save() +{ + if(ui->m_lpName->text().isEmpty()) + { + QMessageBox::critical(this, "Save", "No name given for Partlist."); + return(false); + } + + if(m_id == -1) + saveAs(); + + return(save(m_id)); +} + +bool cPartlistWindow::saveAs() +{ + if(ui->m_lpName->text().isEmpty()) + { + QMessageBox::critical(this, "Save", "No name given for Partlist."); + return(false); + } + + QSqlQuery query; + + query.prepare("SELECT id FROM partlist WHERE name = :name;"); + query.bindValue(":name", ui->m_lpName->text()); + if(!query.exec()) + { + qDebug() << query.lastError().text(); + return(false); + } + + if(query.next()) + { + QMessageBox::critical(this, "Save", "Partlist already exists."); + return(false); + } + + query.prepare("INSERT INTO partlist (name, description) VALUES (:name, :description);"); + query.bindValue(":name", ui->m_lpName->text()); + query.bindValue(":description", ui->m_lpDescription->document()->toPlainText()); + if(!query.exec()) + { + qDebug() << query.lastError().text(); + return(false); + } + + query.prepare("SELECT id FROM partlist WHERE name = :name;"); + query.bindValue(":name", ui->m_lpName->text()); + if(!query.exec()) + { + qDebug() << query.lastError().text(); + return(false); + } + + if(!query.next()) + return(false); + + m_id = query.value("id").toInt(); + + return(save(m_id)); +} + +bool cPartlistWindow::save(qint32 id) +{ + QSqlQuery query; + + query.prepare("DELETE FROM partlistitem WHERE partlistID = :partlistID;"); + query.bindValue(":partlistID", id); + if(!query.exec()) + { + qDebug() << query.lastError().text(); + return(false); + } + + query.prepare("UPDATE partlist SET name=:name, description=:description WHERE id=:id;"); + query.bindValue(":name", ui->m_lpName->text()); + query.bindValue(":description", ui->m_lpDescription->document()->toPlainText()); + query.bindValue(":id", id); + if(!query.exec()) + { + qDebug() << query.lastError().text(); + return(false); + } + + return(true); +} + +bool cPartlistWindow::somethingChanged() +{ + return(m_bSomethingChanged); +} diff --git a/cpartlistwindow.h b/cpartlistwindow.h index 8915657..ad578cf 100644 --- a/cpartlistwindow.h +++ b/cpartlistwindow.h @@ -2,6 +2,7 @@ #define CPARTLISTWINDOW_H #include +#include namespace Ui { class cPartlistWindow; @@ -15,8 +16,31 @@ public: explicit cPartlistWindow(QWidget *parent = 0); ~cPartlistWindow(); + void setMainTab(QTabWidget* lpMainTab); + + void setPartlistName(const QString& szPartlistName); + QString partlistName(); + + void setPartlistID(const qint32& id); + + bool close(); + bool save(); + bool saveAs(); + + bool somethingChanged(); +private slots: + void on_m_lpName_textChanged(const QString &arg1); + +signals: + void partlistChanged(QWidget* lpWidget) const; + private: Ui::cPartlistWindow *ui; + QTabWidget* m_lpMainTab; + qint32 m_id; + bool m_bSomethingChanged; + + bool save(qint32 id); }; #endif // CPARTLISTWINDOW_H diff --git a/cpartlistwindow.ui b/cpartlistwindow.ui index 4eb65ac..502db6a 100644 --- a/cpartlistwindow.ui +++ b/cpartlistwindow.ui @@ -6,26 +6,101 @@ 0 0 - 593 - 560 + 793 + 700 Form - - - - 130 - 190 - 75 - 23 - - - - PushButton - - + + + + + + + + + + + + + Name: + + + + + + + + + + + + + + Costs: + + + + + + + true + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + Description: + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + + + + + + + + + Parts + + + + + + + + + + + diff --git a/cpartwindow.cpp b/cpartwindow.cpp index 4f4d293..c3c8a60 100644 --- a/cpartwindow.cpp +++ b/cpartwindow.cpp @@ -1,15 +1,271 @@ #include "cpartwindow.h" #include "ui_cpartwindow.h" +#include "cparteditdialog.h" + +#include +#include +#include + +#include + +#include +#include + cPartWindow::cPartWindow(QWidget *parent) : QWidget(parent), ui(new Ui::cPartWindow) { ui->setupUi(this); + + m_lpPartListModel = new QStandardItemModel(0, 2); + ui->m_lpPartList->setModel(m_lpPartListModel); } cPartWindow::~cPartWindow() { delete ui; } + +void cPartWindow::setList(cPartGroupList* lpPartGroupList, cPartList* lpPartList) +{ + m_lpPartGroupList = lpPartGroupList; + m_lpPartList = lpPartList; + + showPartList(); +} + +void cPartWindow::showPartList() +{ + m_lpPartListModel->clear(); + + QStringList header; + header << tr("Name") << tr("description") << tr("link"); + + m_lpPartListModel->setHorizontalHeaderLabels(header); + + qint32 iOldPartGroupID = -1; + QStandardItem* lpGroupItem = 0; + + for(int x = 0;x < m_lpPartList->count();x++) + { + QList lpItems; + cPart* lpPart = m_lpPartList->at(x); + cPartGroup* lpPartGroup = lpPart->partGroup(); + + if(lpPartGroup->id() != iOldPartGroupID) + { + iOldPartGroupID = lpPartGroup->id(); + lpGroupItem = new QStandardItem(lpPartGroup->name()); + m_lpPartListModel->appendRow(lpGroupItem); + } + + for(int z = 0;z < header.count();z++) + lpItems.append(new QStandardItem); + + lpItems.at(0)->setText(lpPart->name()); + lpItems.at(1)->setText(lpPart->description()); + lpItems.at(2)->setText(lpPart->link()); + + for(int z = 0;z < header.count();z++) + lpItems.at(z)->setData(QVariant::fromValue(lpPart), Qt::UserRole); + + lpGroupItem->appendRow(lpItems); + } + + for(int z = 0;z < header.count();z++) + ui->m_lpPartList->resizeColumnToContents(z); + + ui->m_lpPartList->expandAll(); + + for(int z = 0;z < header.count();z++) + ui->m_lpPartList->resizeColumnToContents(z); +} + +bool cPartWindow::somethingSelected() +{ + if(ui->m_lpPartList->selectionModel()->selectedRows().count()) + return(true); + return(false); +} + +bool cPartWindow::groupSelected() +{ + QStandardItem* lpItem = m_lpPartListModel->itemFromIndex(ui->m_lpPartList->selectionModel()->currentIndex()); + if(!lpItem) + return(false); + + if(lpItem->parent()) + return(false); + return(true); +} + +void cPartWindow::on_m_lpPartList_clicked(const QModelIndex &index) +{ + selectionChanged(index); +} + +void cPartWindow::on_m_lpPartList_customContextMenuRequested(const QPoint &pos) +{ + QMenu* lpMenu = new QMenu(this); + + lpMenu->addAction("add", this, SLOT(onAdd())); + + if(somethingSelected()) + { + if(!groupSelected()) + { + lpMenu->addAction("edit", this, SLOT(onEdit())); + lpMenu->addAction("delete", this, SLOT(onDelete())); + } + } + + lpMenu->popup(ui->m_lpPartList->viewport()->mapToGlobal(pos)); +} + +void cPartWindow::addPart() +{ + onAdd(); +} + +void cPartWindow::editPart() +{ + onEdit(); +} + +void cPartWindow::deletePart() +{ + onDelete(); +} + +void cPartWindow::onAdd() +{ + cPartEditDialog* lpDialog = new cPartEditDialog(this); + lpDialog->setValues(0, m_lpPartGroupList); + + if(lpDialog->exec() == QDialog::Rejected) + { + delete lpDialog; + return; + } + + qint32 id = lpDialog->id(); + + delete lpDialog; + + partChanged(0); + showPartList(); + + for(int x = 0;x < m_lpPartListModel->rowCount();x++) + { + QStandardItem* lpItem = m_lpPartListModel->item(x, 0); + if(!lpItem) + continue; + + cPart* lpPart = qvariant_cast(lpItem->data(Qt::UserRole)); + if(!lpPart) + continue; + + if(lpPart->id() == id) + { + ui->m_lpPartList->setCurrentIndex(lpItem->index()); + ui->m_lpPartList->scrollTo(lpItem->index()); + return; + } + } +} + +void cPartWindow::onEdit() +{ + if(!ui->m_lpPartList->selectionModel()->selectedRows().count()) + return; + + QStandardItem* lpItem = m_lpPartListModel->itemFromIndex(ui->m_lpPartList->selectionModel()->selectedIndexes().at(0)); + if(!lpItem) + return; + + cPart* lpPart = qvariant_cast(lpItem->data(Qt::UserRole)); + if(!lpPart) + return; + + cPartEditDialog* lpDialog = new cPartEditDialog(this); + lpDialog->setValues(lpPart, m_lpPartGroupList); + + if(lpDialog->exec() == QDialog::Rejected) + { + delete lpDialog; + return; + } + + qint32 id = lpDialog->id(); + + delete lpDialog; + + partChanged(0); + showPartList(); + + for(int x = 0;x < m_lpPartListModel->rowCount();x++) + { + QStandardItem* lpItem = m_lpPartListModel->item(x, 0); + if(!lpItem) + continue; + + cPart* lpPart = qvariant_cast(lpItem->data(Qt::UserRole)); + if(!lpPart) + continue; + + if(lpPart->id() == id) + { + ui->m_lpPartList->setCurrentIndex(lpItem->index()); + ui->m_lpPartList->scrollTo(lpItem->index()); + return; + } + } +} + +void cPartWindow::onDelete() +{ + for(int x = 0;x < ui->m_lpPartList->selectionModel()->selectedIndexes().count();x++) + { + if(ui->m_lpPartList->selectionModel()->selectedIndexes().at(x).column()) + continue; + + QStandardItem* lpItem = m_lpPartListModel->itemFromIndex(ui->m_lpPartList->selectionModel()->selectedIndexes().at(x)); + if(!lpItem) + continue; + + cPart* lpPart = qvariant_cast(lpItem->data(Qt::UserRole)); + if(!lpPart) + continue; + + QSqlQuery query; + QString szQuery; + + szQuery = QString("SELECT * FROM partlistitem WHERE partID = %1;").arg(lpPart->id()); + if(!query.exec(szQuery)) + { + qDebug() << query.lastError().text(); + continue; + } + + if(query.next()) + { + QMessageBox::critical(this, "ERROR", QString("Part '%1' still in use.\nUnable to delete.").arg(lpPart->name())); + continue; + } + + if(QMessageBox::question(this, "DELETE", QString("Are you sure to delete part '%1'?").arg(lpPart->name())) == QMessageBox::No) + continue; + + szQuery = QString("DELETE FROM part WHERE id=%1;").arg(lpPart->id()); + if(!query.exec(szQuery)) + { + qDebug() << query.lastError().text(); + continue; + } + } + + partChanged(0); + showPartList(); +} diff --git a/cpartwindow.h b/cpartwindow.h index 230118d..1270ccb 100644 --- a/cpartwindow.h +++ b/cpartwindow.h @@ -1,7 +1,13 @@ #ifndef CPARTWINDOW_H #define CPARTWINDOW_H + +#include "cpart.h" +#include "cpartgroup.h" + #include +#include + namespace Ui { class cPartWindow; @@ -15,8 +21,33 @@ public: explicit cPartWindow(QWidget *parent = 0); ~cPartWindow(); + void setList(cPartGroupList* lpPartGroupList, cPartList* lpPartList); + bool somethingSelected(); + bool groupSelected(); + + void addPart(); + void editPart(); + void deletePart(); +private slots: + void on_m_lpPartList_clicked(const QModelIndex &index); + void on_m_lpPartList_customContextMenuRequested(const QPoint &pos); + + void onAdd(); + void onEdit(); + void onDelete(); + +signals: + void selectionChanged(const QModelIndex& index) const; + void partGroupChanged(cPartGroup* lpPartGroup) const; + void partChanged(cPart* lpPart) const; + private: - Ui::cPartWindow *ui; + Ui::cPartWindow* ui; + QStandardItemModel* m_lpPartListModel; + cPartGroupList* m_lpPartGroupList; + cPartList* m_lpPartList; + + void showPartList(); }; #endif // CPARTWINDOW_H diff --git a/cpartwindow.ui b/cpartwindow.ui index 27323f3..5d31acc 100644 --- a/cpartwindow.ui +++ b/cpartwindow.ui @@ -15,7 +15,20 @@ - + + + Qt::CustomContextMenu + + + QAbstractItemView::NoEditTriggers + + + true + + + QAbstractItemView::ExtendedSelection + + diff --git a/qtPartlist.pro b/qtPartlist.pro index cbafa85..a121106 100644 --- a/qtPartlist.pro +++ b/qtPartlist.pro @@ -31,7 +31,10 @@ SOURCES += \ cdistributorwindow.cpp \ cpartwindow.cpp \ cdistributor.cpp \ - cdistributoreditdialog.cpp + cdistributoreditdialog.cpp \ + cpartgroup.cpp \ + cpart.cpp \ + cparteditdialog.cpp HEADERS += \ cmainwindow.h \ @@ -40,11 +43,15 @@ HEADERS += \ cdistributorwindow.h \ cpartwindow.h \ cdistributor.h \ - cdistributoreditdialog.h + cdistributoreditdialog.h \ + cpartgroup.h \ + cpart.h \ + cparteditdialog.h FORMS += \ cmainwindow.ui \ cpartlistwindow.ui \ cdistributorwindow.ui \ cpartwindow.ui \ - cdistributoreditdialog.ui + cdistributoreditdialog.ui \ + cparteditdialog.ui diff --git a/qtPartlist.pro.user b/qtPartlist.pro.user index 2704b8b..bc81bc4 100644 --- a/qtPartlist.pro.user +++ b/qtPartlist.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId