OSDN Git Service

[denncoCreator] Implemented cell code manager dialog. The implementation is incomplet...
[dennco/denncoCreator.git] / Source / dialog / dcrenamecelldialog.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 Oct-19, 2012.
18 //
19 #include "dcrenamecelldialog.h"
20
21 #include "dccontainer.h"
22 #include "dccreator.h"
23 #include "dccell.h"
24 #include "dcscene.h"
25 #include "dcvcpage.h"
26 #include "command/dccommand.h"
27 #include "utils/dcqtitemmodel.h"
28 #include "dcsinglecolumntableview.h"
29 #include "DNUtils.h"
30
31 #include <QLineEdit>
32 #include <QTableView>
33 #include <QHeaderView>
34
35   DCRenameCellDialog::DCRenameCellDialog(DCContainer *container, DCCreator *creator, DCCell *cell, QWidget *parent) :
36       QDialog(parent), d_container(container), d_creator(creator), d_cell(cell)
37 {
38     setWindowTitle(tr("Rename cell"));
39
40     d_textField = new QLineEdit;
41     d_textField->setText(QString::fromStdString(cell->getName()));
42
43     d_table = new DCSingleColumnTableView();
44     QStringList headers;
45     headers << "page";
46     d_tableModel = new DCQtItemModel(headers);
47     d_tableModel->setReadOnly(0,true);
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     QString path = QString::fromStdString(cell->getLocation());
55     while (i.hasNext())
56     {
57         i.next();
58         QString item = i.key();
59         d_tableModel->insertString(item);
60         if (item == path) selection = row;
61         row++;
62     }
63     d_table->setModel(d_tableModel);
64
65     setLayout(new QVBoxLayout);
66     d_table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
67
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);
77
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);
89
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*)));
95
96     if (selection >= 0)
97     {
98         QModelIndex index = d_tableModel->index(selection, 0);
99         if (index.isValid())
100             d_table->setCurrentIndex(index);
101     }
102 }
103
104 DCRenameCellDialog::~DCRenameCellDialog()
105 {
106     if (d_tableModel)
107         d_tableModel->deleteLater();
108 }
109
110 bool DCRenameCellDialog::checkInput()
111 {
112     d_message->setText("");
113     d_statusText->setText("");
114     if (d_selectedPagePath.length() == 0)
115     {
116         d_message->setText(tr("Select a page to place the cell"));
117     }
118     else if (d_textField->text().length() > 0)
119     {
120         std::string path = getFQNString(d_selectedPagePath.toLocal8Bit().data(), d_textField->text().toLocal8Bit().data());
121         TKCell *cell = d_container->getCell(path);
122         if (cell)
123         {
124             if (cell != d_cell)
125                 d_message->setText(tr("The cell name exists"));
126         }
127         else
128         {
129             d_statusText->setText(QString::fromStdString(path));
130             d_okButton->setEnabled(true);
131             d_okButton->setDefault(true);
132             return true;
133         }
134     }
135     else
136     {
137         d_message->setText(tr("Input cell name"));
138     }
139     d_okButton->setEnabled(false);
140     d_cancelButton->setDefault(true);
141     return false;
142 }
143
144 void DCRenameCellDialog::textInputChanged(const QString &text)
145 {
146     checkInput();
147 }
148
149 void DCRenameCellDialog::okButtonClicked()
150 {
151     if (checkInput())
152     {
153         d_creator->doCommandRenameCell(this, d_cell, d_selectedPagePath, d_textField->text());
154     }
155 }
156
157 void DCRenameCellDialog::cancelButtonClicked()
158 {
159     done(false);
160 }
161
162
163 void DCRenameCellDialog::listSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
164 {
165     if (selected.count()>0)
166     {
167         d_selectedPagePath = d_tableModel->data(selected.at(0).indexes().at(0), Qt::DisplayRole).toString();
168     }
169     else
170     {
171         d_selectedPagePath = "";
172     }
173     checkInput();
174 }
175
176 void DCRenameCellDialog::commandExecuted(const QUndoCommand *executedCommand)
177 {
178     const DCCommand *command = dynamic_cast<const DCCommand*>(executedCommand);
179     if (command && command->getRequester() == this)
180     {
181         done(true);
182     }
183 }