OSDN Git Service

04db055d49537cbfe05b39400d7ff712f3b95bb6
[dennco/denncoCreator.git] / Source / dialog / dcaddcelldialog.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 "dcaddcelldialog.h"
20
21 #include "dccontainer.h"
22 #include "dccreator.h"
23 #include "dcscene.h"
24 #include "dcvcpage.h"
25 #include "command/dccommand.h"
26 #include "utils/dcqtitemmodel.h"
27 #include "dcsinglecolumntableview.h"
28 #include "DNUtils.h"
29
30 #include <QLineEdit>
31 #include <QTableView>
32 #include <QHeaderView>
33
34   DCAddCellDialog::DCAddCellDialog(DCContainer *container, DCCreator *creator, const QString& path, float pageX, float pageY, QWidget *parent) :
35       QDialog(parent), d_container(container), d_creator(creator), d_pageX(pageX), d_pageY(pageY)
36 {
37     setWindowTitle(tr("Add cell"));
38
39     d_textField = new QLineEdit;
40
41     d_table = new DCSingleColumnTableView();
42     QStringList headers;
43     headers << "page";
44     d_tableModel = new DCQtItemModel(headers);
45     d_tableModel->setReadOnly(0,true);
46
47     d_comboBox = new QComboBox;
48     d_comboBox->addItems(QStringList(d_container->getAvailableCellTypes()));
49
50     DCScene *scene = container->getScene();
51     const QMap<QString,DCVCPage*> *pages = scene->getPages();
52     QMapIterator<QString, DCVCPage*> i(*pages);
53     int row = 0;
54     int selection = -1;
55     while (i.hasNext())
56     {
57         i.next();
58         QString item = i.key();
59         d_tableModel->insertString(item);
60         if (item == path) selection = row;
61         row++;
62     }
63     d_table->setModel(d_tableModel);
64
65     setLayout(new QVBoxLayout);
66     d_table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
67
68     d_okButton = new QPushButton(tr("OK"));
69     d_cancelButton = new QPushButton(tr("Cancel"));
70     QHBoxLayout *buttonLayout = new QHBoxLayout;
71     buttonLayout->addItem(new QSpacerItem(10,10, QSizePolicy::Expanding));
72     buttonLayout->addWidget(d_cancelButton);
73     buttonLayout->addWidget(d_okButton);
74     d_okButton->setEnabled(false);
75     d_cancelButton->setAutoDefault(true);
76     d_cancelButton->setDefault(true);
77
78     QGridLayout *glayout = new QGridLayout;
79     glayout->addWidget(new QLabel(tr("name")),0,0);
80     glayout->addWidget(d_textField,0,1);
81     glayout->addWidget(new QLabel(tr("type")),1,0);
82     glayout->addWidget(d_comboBox);
83     ((QVBoxLayout*)layout())->addLayout(glayout);
84     layout()->addWidget(d_table);
85     d_statusText = new QLabel;
86     d_message = new QLabel;
87     d_message->setStyleSheet("color : red;");
88     layout()->addWidget(d_statusText);
89     layout()->addWidget(d_message);
90     ((QVBoxLayout*)layout())->addLayout(buttonLayout);
91
92     connect(d_textField, SIGNAL(textChanged(QString)), this, SLOT(textInputChanged(QString)));
93     connect(d_okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
94     connect(d_cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClicked()));
95     connect(d_table,SIGNAL(selectionChangedSignal(QItemSelection,QItemSelection)), this, SLOT(listSelectionChanged(QItemSelection,QItemSelection)));
96     connect(d_creator, SIGNAL(commandExecuted(const QUndoCommand*)), this, SLOT(commandExecuted(const QUndoCommand*)));
97
98     if (selection >= 0)
99     {
100         QModelIndex index = d_tableModel->index(selection, 0);
101         if (index.isValid())
102             d_table->setCurrentIndex(index);
103     }
104 }
105
106 DCAddCellDialog::~DCAddCellDialog()
107 {
108     if (d_tableModel)
109         d_tableModel->deleteLater();
110 }
111
112 bool DCAddCellDialog::checkInput()
113 {
114     d_message->setText("");
115     d_statusText->setText("");
116     if (d_selectedPagePath.length() == 0)
117     {
118         d_message->setText(tr("Select a page to place the cell"));
119     }
120     else if (d_textField->text().length() > 0)
121     {
122         std::string path = getFQNString(d_selectedPagePath.toLocal8Bit().data(), d_textField->text().toLocal8Bit().data());
123         TKCell *cell = d_container->getCell(path);
124         if (cell)
125         {
126             d_message->setText(tr("The cell name exists"));
127         }
128         else
129         {
130             d_statusText->setText(QString::fromStdString(path));
131             d_okButton->setEnabled(true);
132             d_okButton->setDefault(true);
133             return true;
134         }
135     }
136     else
137     {
138         d_message->setText(tr("Input cell name"));
139     }
140     d_okButton->setEnabled(false);
141     d_cancelButton->setDefault(true);
142     return false;
143 }
144
145 void DCAddCellDialog::textInputChanged(const QString &text)
146 {
147     checkInput();
148 }
149
150 void DCAddCellDialog::okButtonClicked()
151 {
152     if (checkInput())
153     {
154         d_creator->doCommandAddCell(this, d_container, d_selectedPagePath, d_textField->text(), d_comboBox->currentText(), d_pageX, d_pageY);
155     }
156
157 }
158
159 void DCAddCellDialog::cancelButtonClicked()
160 {
161     done(false);
162 }
163
164
165 void DCAddCellDialog::listSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
166 {
167     if (selected.count()>0)
168     {
169         d_selectedPagePath = d_tableModel->data(selected.at(0).indexes().at(0), Qt::DisplayRole).toString();
170     }
171     else
172     {
173         d_selectedPagePath = "";
174     }
175     checkInput();
176 }
177
178 void DCAddCellDialog::commandExecuted(const QUndoCommand *executedCommand)
179 {
180     const DCCommand *command = dynamic_cast<const DCCommand*>(executedCommand);
181     if (command && command->getRequester() == this)
182     {
183         done(true);
184     }
185 }