OSDN Git Service

[denncoCreator] cell code scripts editor modification. Set editor readonly when a...
[dennco/denncoCreator.git] / Source / dialog / dcaddcellcodeclassdialog.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 "dcaddcellcodeclassdialog.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 DCAddCellCodeClassDialog::DCAddCellCodeClassDialog(DCContainer *container, DCCreator *creator, const QString& path, QWidget *parent) :
34     QDialog(parent), d_container(container), d_creator(creator)
35 {
36     setWindowTitle(tr("Add class"));
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->addItems(QStringList(d_container->getAvailableCellTypes()));
48
49     DCScene *scene = container->getScene();
50     const QMap<QString,DCVCPage*> *pages = scene->getPages();
51     QMapIterator<QString, DCVCPage*> i(*pages);
52     int row = 0;
53     int selection = -1;
54     while (i.hasNext())
55     {
56         i.next();
57         QString item = i.key();
58         d_tableModel->insertString(item);
59         if (item == path) selection = row;
60         row++;
61     }
62     d_table->setModel(d_tableModel);
63
64     setLayout(new QVBoxLayout);
65     d_table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
66
67     d_okButton = new QPushButton(tr("OK"));
68     d_cancelButton = new QPushButton(tr("Cancel"));
69     QHBoxLayout *buttonLayout = new QHBoxLayout;
70     buttonLayout->addItem(new QSpacerItem(10,10, QSizePolicy::Expanding));
71     buttonLayout->addWidget(d_cancelButton);
72     buttonLayout->addWidget(d_okButton);
73     d_okButton->setEnabled(false);
74     d_cancelButton->setAutoDefault(true);
75     d_cancelButton->setDefault(true);
76
77     QGridLayout *glayout = new QGridLayout;
78     glayout->addWidget(new QLabel(tr("name")),0,0);
79     glayout->addWidget(d_textField,0,1);
80     glayout->addWidget(new QLabel(tr("type")),1,0);
81     glayout->addWidget(d_comboBox);
82     ((QVBoxLayout*)layout())->addLayout(glayout);
83     layout()->addWidget(d_table);
84     d_statusText = new QLabel;
85     d_message = new QLabel;
86     d_message->setStyleSheet("color : red;");
87     layout()->addWidget(d_statusText);
88     layout()->addWidget(d_message);
89     ((QVBoxLayout*)layout())->addLayout(buttonLayout);
90
91     connect(d_textField, SIGNAL(textChanged(QString)), this, SLOT(textInputChanged(QString)));
92     connect(d_okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
93     connect(d_cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClicked()));
94     connect(d_table,SIGNAL(selectionChangedSignal(QItemSelection,QItemSelection)), this, SLOT(listSelectionChanged(QItemSelection,QItemSelection)));
95     connect(d_creator, SIGNAL(commandExecuted(const QUndoCommand*)), this, SLOT(commandExecuted(const QUndoCommand*)));
96
97     if (selection >= 0)
98     {
99         d_table->selectRow(selection);
100     }
101 }
102
103 DCAddCellCodeClassDialog::~DCAddCellCodeClassDialog()
104 {
105 }
106
107
108 bool DCAddCellCodeClassDialog::checkInput()
109 {
110     d_message->setText("");
111     d_statusText->setText("");
112     if (d_selectedPagePath.length() == 0)
113     {
114         d_message->setText(tr("Select a page to place the class"));
115     }
116     else if (d_textField->text().length() > 0)
117     {
118         QString path = d_selectedPagePath;
119         path.append("#");
120         path.append(d_textField->text());
121         TKCellCode *cellCode = d_container->getCellCode(path.toStdString());
122         if (cellCode)
123         {
124             d_message->setText(tr("The class name exists"));
125         }
126         else
127         {
128             d_okButton->setEnabled(true);
129             d_statusText->setText(path);
130             return true;
131         }
132     }
133     else
134     {
135         d_message->setText(tr("Input class name"));
136     }
137     d_okButton->setEnabled(false);
138     return false;
139 }
140
141 void DCAddCellCodeClassDialog::textInputChanged(const QString &text)
142 {
143     checkInput();
144 }
145
146 void DCAddCellCodeClassDialog::okButtonClicked()
147 {
148     if (checkInput())
149     {
150         QString path = d_selectedPagePath;
151         path.append("#");
152         path.append(d_textField->text());
153         d_creator->doCommandAddCellCodeClass(this, d_container, path, d_comboBox->currentText());
154     }
155
156 }
157
158 void DCAddCellCodeClassDialog::cancelButtonClicked()
159 {
160     done(false);
161 }
162
163
164 void DCAddCellCodeClassDialog::listSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
165 {
166     if (selected.count()>0)
167     {
168         d_selectedPagePath = d_tableModel->data(selected.at(0).indexes().at(0), Qt::DisplayRole).toString();
169     }
170     else
171     {
172         d_selectedPagePath = "";
173     }
174     checkInput();
175 }
176
177 QString DCAddCellCodeClassDialog::getAddedCellCodeClassName() const
178 {
179     if (d_selectedPagePath.length() == 0)
180     {
181         return "";
182     }
183     else if (d_textField->text().length() > 0)
184     {
185         QString path = d_selectedPagePath;
186         path.append("#");
187         path.append(d_textField->text());
188         return path;
189     }
190     return "";
191 }
192
193 void DCAddCellCodeClassDialog::commandExecuted(const QUndoCommand *executedCommand)
194 {
195     const DCCommand *command = dynamic_cast<const DCCommand*>(executedCommand);
196     if (command && command->getRequester() == this)
197     {
198         done(true);
199     }
200 }