OSDN Git Service

f7881d3685e142ab250194d107a693164dd927a7
[dennco/denncoCreator.git] / Source / dialog / dcassigncellcodeclassdialog.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
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.
7 //
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.
12 //
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/>.
15
16 //
17 //  Created by tkawata on Sep-30, 2012.
18 //
19 #include "dcassigncellcodeclassdialog.h"
20
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"
27
28 #include <QTableView>
29 #include <QVBoxLayout>
30 #include <QPushButton>
31 #include <QHeaderView>
32
33 DCAssignCellCodeClassDialog::DCAssignCellCodeClassDialog(DCCreator *creator, QWidget *parent) :
34     QDialog(parent), d_creator(creator), d_selectedCellCodeClassName("")
35 {
36     setWindowTitle(tr("Assign cell code class"));
37
38     d_container = d_creator->getCurrentContainer();
39     d_table = new DCSingleColumnTableView();
40     QStringList headers;
41     headers << "page # name";
42     d_tableModel = new DCQtItemModel(headers);
43     d_tableModel->setReadOnly(0,true);
44
45     updateClassList();
46
47     d_table->setModel(d_tableModel);
48
49     d_addNewButton = new QPushButton(tr("Add new"));
50     setLayout(new QVBoxLayout);
51     d_table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
52
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);
62
63     layout()->addWidget(d_table);
64     layout()->addWidget(d_addNewButton);
65     ((QVBoxLayout*)layout())->addLayout(buttonLayout);
66
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)));
71
72 }
73
74
75 DCAssignCellCodeClassDialog::~DCAssignCellCodeClassDialog()
76 {
77     if (d_tableModel)
78         d_tableModel->deleteLater();
79 }
80
81 void DCAssignCellCodeClassDialog::updateClassList()
82 {
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)
88         {
89             QStringList items;
90             items << QString::fromStdString(it->first);
91             d_tableModel->insertStringList(items);
92         }
93     }
94 }
95
96 DCCellCode* DCAssignCellCodeClassDialog::getSelectedCellCodeClass() const
97 {
98     if (d_selectedCellCodeClassName.length()>0)
99         return dynamic_cast<DCCellCode*>(d_container->getCellCode(d_selectedCellCodeClassName.toStdString()));
100     else
101         return NULL;
102
103 }
104
105 void DCAssignCellCodeClassDialog::okButtonClicked()
106 {
107     done(true);
108 }
109
110 void DCAssignCellCodeClassDialog::cancelButtonClicked()
111 {
112     d_selectedCellCodeClassName = "";
113     done(false);
114 }
115
116 void DCAssignCellCodeClassDialog::addNewClicked()
117 {
118     DCAddCellCodeClassDialog dialog(d_creator, "");
119     dialog.exec();
120     updateClassList();
121     QString classname = dialog.getAddedCellCodeClassName();
122     if (classname.length()>0)
123     {
124         int cnt = d_tableModel->rowCount();
125         for (int i = 0; i < cnt; i++)
126         {
127             QString s = d_tableModel->data(d_tableModel->index(i,0), Qt::DisplayRole).toString();
128             if (s == classname)
129             {
130                 d_table->selectRow(i);
131                 break;
132             }
133         }
134     }
135 }
136
137 void DCAssignCellCodeClassDialog::listSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
138 {
139     if (selected.count()>0)
140     {
141         d_selectedCellCodeClassName = d_tableModel->data(selected.at(0).indexes().at(0), Qt::DisplayRole).toString();
142         d_okButton->setEnabled(true);
143     }
144     else
145     {
146         d_okButton->setEnabled(false);
147     }
148 }