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 "dcaddcelldialog.h"
21 #include "dccontainer.h"
22 #include "dccreator.h"
25 #include "command/dccommand.h"
26 #include "utils/dcqtitemmodel.h"
27 #include "dcsinglecolumntableview.h"
32 #include <QHeaderView>
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)
37 setWindowTitle(tr("Add cell"));
39 d_textField = new QLineEdit;
41 d_table = new DCSingleColumnTableView();
44 d_tableModel = new DCQtItemModel(headers);
45 d_tableModel->setReadOnly(0,true);
47 d_comboBox = new QComboBox;
48 d_comboBox->addItems(QStringList(d_container->getAvailableCellTypes()));
50 DCScene *scene = container->getScene();
51 const QMap<QString,DCVCPage*> *pages = scene->getPages();
52 QMapIterator<QString, DCVCPage*> i(*pages);
58 QString item = i.key();
59 d_tableModel->insertString(item);
60 if (item == path) selection = row;
63 d_table->setModel(d_tableModel);
65 setLayout(new QVBoxLayout);
66 d_table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
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);
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);
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*)));
100 QModelIndex index = d_tableModel->index(selection, 0);
102 d_table->setCurrentIndex(index);
106 DCAddCellDialog::~DCAddCellDialog()
109 d_tableModel->deleteLater();
112 bool DCAddCellDialog::checkInput()
114 d_message->setText("");
115 d_statusText->setText("");
116 if (d_selectedPagePath.length() == 0)
118 d_message->setText(tr("Select a page to place the cell"));
120 else if (d_textField->text().length() > 0)
122 std::string path = getFQNString(d_selectedPagePath.toLocal8Bit().data(), d_textField->text().toLocal8Bit().data());
123 TKCell *cell = d_container->getCell(path);
126 d_message->setText(tr("The cell name exists"));
130 d_statusText->setText(QString::fromStdString(path));
131 d_okButton->setEnabled(true);
132 d_okButton->setDefault(true);
138 d_message->setText(tr("Input cell name"));
140 d_okButton->setEnabled(false);
141 d_cancelButton->setDefault(true);
145 void DCAddCellDialog::textInputChanged(const QString &text)
150 void DCAddCellDialog::okButtonClicked()
154 d_creator->doCommandAddCell(this, d_container, d_selectedPagePath, d_textField->text(), d_comboBox->currentText(), d_pageX, d_pageY);
159 void DCAddCellDialog::cancelButtonClicked()
165 void DCAddCellDialog::listSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
167 if (selected.count()>0)
169 d_selectedPagePath = d_tableModel->data(selected.at(0).indexes().at(0), Qt::DisplayRole).toString();
173 d_selectedPagePath = "";
178 void DCAddCellDialog::commandExecuted(const QUndoCommand *executedCommand)
180 const DCCommand *command = dynamic_cast<const DCCommand*>(executedCommand);
181 if (command && command->getRequester() == this)