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 Oct-19, 2012.
19 #include "dcrenamecelldialog.h"
21 #include "dccontainer.h"
22 #include "dccreator.h"
26 #include "command/dccommand.h"
27 #include "utils/dcqtitemmodel.h"
28 #include "dcsinglecolumntableview.h"
33 #include <QHeaderView>
35 DCRenameCellDialog::DCRenameCellDialog(DCContainer *container, DCCreator *creator, DCCell *cell, QWidget *parent) :
36 QDialog(parent), d_container(container), d_creator(creator), d_cell(cell)
38 setWindowTitle(tr("Rename cell"));
40 d_textField = new QLineEdit;
41 d_textField->setText(QString::fromStdString(cell->getName()));
43 d_table = new DCSingleColumnTableView();
46 d_tableModel = new DCQtItemModel(headers);
47 d_tableModel->setReadOnly(0,true);
49 DCScene *scene = container->getScene();
50 const QMap<QString,DCVCPage*> *pages = scene->getPages();
51 QMapIterator<QString, DCVCPage*> i(*pages);
54 QString path = QString::fromStdString(cell->getLocation());
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 ((QVBoxLayout*)layout())->addLayout(glayout);
82 layout()->addWidget(d_table);
83 d_statusText = new QLabel;
84 d_message = new QLabel;
85 d_message->setStyleSheet("color : red;");
86 layout()->addWidget(d_statusText);
87 layout()->addWidget(d_message);
88 ((QVBoxLayout*)layout())->addLayout(buttonLayout);
90 connect(d_textField, SIGNAL(textChanged(QString)), this, SLOT(textInputChanged(QString)));
91 connect(d_okButton, SIGNAL(clicked()), this, SLOT(okButtonClicked()));
92 connect(d_cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClicked()));
93 connect(d_table,SIGNAL(selectionChangedSignal(QItemSelection,QItemSelection)), this, SLOT(listSelectionChanged(QItemSelection,QItemSelection)));
94 connect(d_creator, SIGNAL(commandExecuted(const QUndoCommand*)), this, SLOT(commandExecuted(const QUndoCommand*)));
98 QModelIndex index = d_tableModel->index(selection, 0);
100 d_table->setCurrentIndex(index);
104 DCRenameCellDialog::~DCRenameCellDialog()
109 bool DCRenameCellDialog::checkInput()
111 d_message->setText("");
112 d_statusText->setText("");
113 if (d_selectedPagePath.length() == 0)
115 d_message->setText(tr("Select a page to place the cell"));
117 else if (d_textField->text().length() > 0)
119 std::string path = getFQNString(d_selectedPagePath.toLocal8Bit().data(), d_textField->text().toLocal8Bit().data());
120 TKCell *cell = d_container->getCell(path);
124 d_message->setText(tr("The cell name exists"));
128 d_statusText->setText(QString::fromStdString(path));
129 d_okButton->setEnabled(true);
130 d_okButton->setDefault(true);
136 d_message->setText(tr("Input cell name"));
138 d_okButton->setEnabled(false);
139 d_cancelButton->setDefault(true);
143 void DCRenameCellDialog::textInputChanged(const QString &text)
148 void DCRenameCellDialog::okButtonClicked()
152 d_creator->doCommandRenameCell(this, d_cell, d_selectedPagePath, d_textField->text());
156 void DCRenameCellDialog::cancelButtonClicked()
162 void DCRenameCellDialog::listSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
164 if (selected.count()>0)
166 d_selectedPagePath = d_tableModel->data(selected.at(0).indexes().at(0), Qt::DisplayRole).toString();
170 d_selectedPagePath = "";
175 void DCRenameCellDialog::commandExecuted(const QUndoCommand *executedCommand)
177 const DCCommand *command = dynamic_cast<const DCCommand*>(executedCommand);
178 if (command && command->getRequester() == this)