OSDN Git Service

[denncoCreator] minor improvements
[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
107 }
108
109 bool DCRenameCellDialog::checkInput()
110 {
111     d_message->setText("");
112     d_statusText->setText("");
113     if (d_selectedPagePath.length() == 0)
114     {
115         d_message->setText(tr("Select a page to place the cell"));
116     }
117     else if (d_textField->text().length() > 0)
118     {
119         std::string path = getFQNString(d_selectedPagePath.toLocal8Bit().data(), d_textField->text().toLocal8Bit().data());
120         TKCell *cell = d_container->getCell(path);
121         if (cell)
122         {
123             if (cell != d_cell)
124                 d_message->setText(tr("The cell name exists"));
125         }
126         else
127         {
128             d_statusText->setText(QString::fromStdString(path));
129             d_okButton->setEnabled(true);
130             d_okButton->setDefault(true);
131             return true;
132         }
133     }
134     else
135     {
136         d_message->setText(tr("Input cell name"));
137     }
138     d_okButton->setEnabled(false);
139     d_cancelButton->setDefault(true);
140     return false;
141 }
142
143 void DCRenameCellDialog::textInputChanged(const QString &text)
144 {
145     checkInput();
146 }
147
148 void DCRenameCellDialog::okButtonClicked()
149 {
150     if (checkInput())
151     {
152         d_creator->doCommandRenameCell(this, d_cell, d_selectedPagePath, d_textField->text());
153     }
154 }
155
156 void DCRenameCellDialog::cancelButtonClicked()
157 {
158     done(false);
159 }
160
161
162 void DCRenameCellDialog::listSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
163 {
164     if (selected.count()>0)
165     {
166         d_selectedPagePath = d_tableModel->data(selected.at(0).indexes().at(0), Qt::DisplayRole).toString();
167     }
168     else
169     {
170         d_selectedPagePath = "";
171     }
172     checkInput();
173 }
174
175 void DCRenameCellDialog::commandExecuted(const QUndoCommand *executedCommand)
176 {
177     const DCCommand *command = dynamic_cast<const DCCommand*>(executedCommand);
178     if (command && command->getRequester() == this)
179     {
180         done(true);
181     }
182 }