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 "dcaddcellcodeclassdialog.h"
21 #include "dccontainer.h"
22 #include "dccreator.h"
25 #include "command/dccommand.h"
26 #include "utils/dcqtitemmodel.h"
27 #include "dcsinglecolumntableview.h"
31 #include <QHeaderView>
33 DCAddCellCodeClassDialog::DCAddCellCodeClassDialog(DCContainer *container, DCCreator *creator, const QString& path, QWidget *parent) :
34 QDialog(parent), d_container(container), d_creator(creator)
36 setWindowTitle(tr("Add class"));
38 d_textField = new QLineEdit;
40 d_table = new DCSingleColumnTableView();
43 d_tableModel = new DCQtItemModel(headers);
44 d_tableModel->setReadOnly(0,true);
46 d_comboBox = new QComboBox;
47 d_comboBox->addItems(QStringList(d_container->getAvailableCellTypes()));
49 DCScene *scene = container->getScene();
50 const QMap<QString,DCVCPage*> *pages = scene->getPages();
51 QMapIterator<QString, DCVCPage*> i(*pages);
57 QString item = i.key();
58 d_tableModel->insertString(item);
59 if (item == path) selection = row;
62 d_table->setModel(d_tableModel);
64 setLayout(new QVBoxLayout);
65 d_table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
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);
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);
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*)));
99 d_table->selectRow(selection);
103 DCAddCellCodeClassDialog::~DCAddCellCodeClassDialog()
108 bool DCAddCellCodeClassDialog::checkInput()
110 d_message->setText("");
111 d_statusText->setText("");
112 if (d_selectedPagePath.length() == 0)
114 d_message->setText(tr("Select a page to place the class"));
116 else if (d_textField->text().length() > 0)
118 QString path = d_selectedPagePath;
120 path.append(d_textField->text());
121 TKCellCode *cellCode = d_container->getCellCode(path.toStdString());
124 d_message->setText(tr("The class name exists"));
128 d_okButton->setEnabled(true);
129 d_statusText->setText(path);
135 d_message->setText(tr("Input class name"));
137 d_okButton->setEnabled(false);
141 void DCAddCellCodeClassDialog::textInputChanged(const QString &text)
146 void DCAddCellCodeClassDialog::okButtonClicked()
150 QString path = d_selectedPagePath;
152 path.append(d_textField->text());
153 d_creator->doCommandAddCellCodeClass(this, d_container, path, d_comboBox->currentText());
158 void DCAddCellCodeClassDialog::cancelButtonClicked()
164 void DCAddCellCodeClassDialog::listSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
166 if (selected.count()>0)
168 d_selectedPagePath = d_tableModel->data(selected.at(0).indexes().at(0), Qt::DisplayRole).toString();
172 d_selectedPagePath = "";
177 QString DCAddCellCodeClassDialog::getAddedCellCodeClassName() const
179 if (d_selectedPagePath.length() == 0)
183 else if (d_textField->text().length() > 0)
185 QString path = d_selectedPagePath;
187 path.append(d_textField->text());
193 void DCAddCellCodeClassDialog::commandExecuted(const QUndoCommand *executedCommand)
195 const DCCommand *command = dynamic_cast<const DCCommand*>(executedCommand);
196 if (command && command->getRequester() == this)