1 // Copyright (c) 2012 Dennco Project
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // Created by tkawata on Sep-30, 2012.
19 #include "dcassigncellcodeclassdialog.h"
21 #include "dccreator.h"
22 #include "dccontainer.h"
23 #include "DCCellCode.h"
24 #include "utils/dcqtitemmodel.h"
25 #include "dcaddcellcodeclassdialog.h"
26 #include "dcsinglecolumntableview.h"
29 #include <QVBoxLayout>
30 #include <QPushButton>
31 #include <QHeaderView>
33 DCAssignCellCodeClassDialog::DCAssignCellCodeClassDialog(DCCreator *creator, QWidget *parent) :
34 QDialog(parent), d_creator(creator), d_selectedCellCodeClassName("")
36 setWindowTitle(tr("Assign cell code class"));
38 d_container = d_creator->getCurrentContainer();
39 d_table = new DCSingleColumnTableView();
41 headers << "page # name";
42 d_tableModel = new DCQtItemModel(headers);
43 d_tableModel->setReadOnly(0,true);
47 d_table->setModel(d_tableModel);
49 d_addNewButton = new QPushButton(tr("Add new"));
50 setLayout(new QVBoxLayout);
51 d_table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
53 d_okButton = new QPushButton(tr("OK"));
54 d_cancelButton = new QPushButton(tr("Cancel"));
55 QHBoxLayout *buttonLayout = new QHBoxLayout;
56 buttonLayout->addItem(new QSpacerItem(10,10, QSizePolicy::Expanding));
57 buttonLayout->addWidget(d_cancelButton);
58 buttonLayout->addWidget(d_okButton);
59 d_okButton->setEnabled(false);
60 d_cancelButton->setAutoDefault(true);
61 d_cancelButton->setDefault(true);
63 layout()->addWidget(d_table);
64 layout()->addWidget(d_addNewButton);
65 ((QVBoxLayout*)layout())->addLayout(buttonLayout);
67 connect(d_okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
68 connect(d_cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClicked()));
69 connect(d_addNewButton, SIGNAL(clicked()), this, SLOT(addNewClicked()));
70 connect(d_table,SIGNAL(selectionChangedSignal(QItemSelection,QItemSelection)), this, SLOT(listSelectionChanged(QItemSelection,QItemSelection)));
75 DCAssignCellCodeClassDialog::~DCAssignCellCodeClassDialog()
78 d_tableModel->deleteLater();
81 void DCAssignCellCodeClassDialog::updateClassList()
83 d_tableModel->removeAllItems();
84 const TKCellCodeMap *cellcodes = d_container->getCellCodes();
85 TKCellCode *emtpyCellCodeClass = d_container->getEmptyCellCodeClass();
86 for ( TKCellCodeMap::const_iterator it = cellcodes->begin(); it != cellcodes->end(); ++it ) {
87 if (it->second != emtpyCellCodeClass)
90 items << QString::fromStdString(it->first);
91 d_tableModel->insertStringList(items);
96 DCCellCode* DCAssignCellCodeClassDialog::getSelectedCellCodeClass() const
98 if (d_selectedCellCodeClassName.length()>0)
99 return dynamic_cast<DCCellCode*>(d_container->getCellCode(d_selectedCellCodeClassName.toStdString()));
105 void DCAssignCellCodeClassDialog::okButtonClicked()
110 void DCAssignCellCodeClassDialog::cancelButtonClicked()
112 d_selectedCellCodeClassName = "";
116 void DCAssignCellCodeClassDialog::addNewClicked()
118 DCAddCellCodeClassDialog dialog(d_creator, "");
121 QString classname = dialog.getAddedCellCodeClassName();
122 if (classname.length()>0)
124 int cnt = d_tableModel->rowCount();
125 for (int i = 0; i < cnt; i++)
127 QString s = d_tableModel->data(d_tableModel->index(i,0), Qt::DisplayRole).toString();
130 d_table->selectRow(i);
137 void DCAssignCellCodeClassDialog::listSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
139 if (selected.count()>0)
141 d_selectedCellCodeClassName = d_tableModel->data(selected.at(0).indexes().at(0), Qt::DisplayRole).toString();
142 d_okButton->setEnabled(true);
146 d_okButton->setEnabled(false);