OSDN Git Service

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