1 /***************************************************************************
2     File                 : LayerDialog.cpp
3     Project              : SciDAVis
4     --------------------------------------------------------------------
5     Copyright            : (C) 2006 by Ion Vasilief, Tilman Benkert
6     Email (use @ for *)  : ion_vasilief*yahoo.fr, thzs*gmx.net
7     Description          : Arrange layers dialog
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  *                                                                         *
13  *  This program is free software; you can redistribute it and/or modify   *
14  *  it under the terms of the GNU General Public License as published by   *
15  *  the Free Software Foundation; either version 2 of the License, or      *
16  *  (at your option) any later version.                                    *
17  *                                                                         *
18  *  This program is distributed in the hope that it will be useful,        *
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
21  *  GNU General Public License for more details.                           *
22  *                                                                         *
23  *   You should have received a copy of the GNU General Public License     *
24  *   along with this program; if not, write to the Free Software           *
25  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
26  *   Boston, MA  02110-1301  USA                                           *
27  *                                                                         *
28  ***************************************************************************/
29 #include "LayerDialog.h"
30 #include "ApplicationWindow.h"
31 
32 #include <QLayout>
33 #include <QSpinBox>
34 #include <QCheckBox>
35 #include <QGroupBox>
36 #include <QPushButton>
37 #include <QLabel>
38 #include <QComboBox>
39 #include <QFontDialog>
40 #include <QFont>
41 #include <QMessageBox>
42 
LayerDialog(QWidget * parent,Qt::WindowFlags fl)43 LayerDialog::LayerDialog(QWidget *parent, Qt::WindowFlags fl) : QDialog(parent, fl)
44 {
45     setWindowTitle(tr("Arrange Layers"));
46 
47     QGroupBox *gb1 = new QGroupBox(tr("Layers"));
48     QGridLayout *gl1 = new QGridLayout(gb1);
49     gl1->addWidget(new QLabel(tr("Number")), 0, 0);
50     layersBox = new QSpinBox();
51     layersBox->setRange(0, 100);
52     gl1->addWidget(layersBox, 0, 1);
53 
54     fitBox = new QCheckBox(tr("Automatic &layout"));
55     fitBox->setChecked(false);
56     gl1->addWidget(fitBox, 1, 1);
57     gl1->setRowStretch(2, 1);
58 
59     QGroupBox *gb2 = new QGroupBox(tr("Alignement"));
60     QGridLayout *gl2 = new QGridLayout(gb2);
61     gl2->addWidget(new QLabel(tr("Horizontal")), 0, 0);
62 
63     alignHorBox = new QComboBox();
64     alignHorBox->addItem(tr("Center"));
65     alignHorBox->addItem(tr("Left"));
66     alignHorBox->addItem(tr("Right"));
67     gl2->addWidget(alignHorBox, 0, 1);
68 
69     gl2->addWidget(new QLabel(tr("Vertical")), 1, 0);
70     alignVertBox = new QComboBox();
71     alignVertBox->addItem(tr("Center"));
72     alignVertBox->addItem(tr("Top"));
73     alignVertBox->addItem(tr("Bottom"));
74     gl2->addWidget(alignVertBox, 1, 1);
75     gl2->setRowStretch(2, 1);
76 
77     GroupGrid = new QGroupBox(tr("Grid"));
78     QGridLayout *gl3 = new QGridLayout(GroupGrid);
79     gl3->addWidget(new QLabel(tr("Columns")), 0, 0);
80     boxX = new QSpinBox();
81     boxX->setRange(1, 100);
82     gl3->addWidget(boxX, 0, 1);
83     gl3->addWidget(new QLabel(tr("Rows")), 1, 0);
84     boxY = new QSpinBox();
85     boxY->setRange(1, 100);
86     gl3->addWidget(boxY, 1, 1);
87 
88     GroupCanvasSize = new QGroupBox(tr("&Layer Canvas Size"));
89     GroupCanvasSize->setCheckable(true);
90     GroupCanvasSize->setChecked(false);
91 
92     QGridLayout *gl5 = new QGridLayout(GroupCanvasSize);
93     gl5->addWidget(new QLabel(tr("Width")), 0, 0);
94     boxCanvasWidth = new QSpinBox();
95     boxCanvasWidth->setRange(0, 10000);
96     boxCanvasWidth->setSingleStep(50);
97     boxCanvasWidth->setSuffix(tr(" pixels"));
98     gl5->addWidget(boxCanvasWidth, 0, 1);
99     gl5->addWidget(new QLabel(tr("Height")), 1, 0);
100     boxCanvasHeight = new QSpinBox();
101     boxCanvasHeight->setRange(0, 10000);
102     boxCanvasHeight->setSingleStep(50);
103     boxCanvasHeight->setSuffix(tr(" pixels"));
104     gl5->addWidget(boxCanvasHeight, 1, 1);
105 
106     QGroupBox *gb4 = new QGroupBox(tr("Spacing"));
107     QGridLayout *gl4 = new QGridLayout(gb4);
108     gl4->addWidget(new QLabel(tr("Columns gap")), 0, 0);
109     boxColsGap = new QSpinBox();
110     boxColsGap->setRange(0, 1000);
111     boxColsGap->setSingleStep(5);
112     boxColsGap->setSuffix(tr(" pixels"));
113     gl4->addWidget(boxColsGap, 0, 1);
114     gl4->addWidget(new QLabel(tr("Rows gap")), 1, 0);
115     boxRowsGap = new QSpinBox();
116     boxRowsGap->setRange(0, 1000);
117     boxRowsGap->setSingleStep(5);
118     boxRowsGap->setSuffix(tr(" pixels"));
119     gl4->addWidget(boxRowsGap, 1, 1);
120     gl4->addWidget(new QLabel(tr("Left margin")), 2, 0);
121     boxLeftSpace = new QSpinBox();
122     boxLeftSpace->setRange(0, 1000);
123     boxLeftSpace->setSingleStep(5);
124     boxLeftSpace->setSuffix(tr(" pixels"));
125     gl4->addWidget(boxLeftSpace, 2, 1);
126     gl4->addWidget(new QLabel(tr("Right margin")), 3, 0);
127     boxRightSpace = new QSpinBox();
128     boxRightSpace->setRange(0, 1000);
129     boxRightSpace->setSingleStep(5);
130     boxRightSpace->setSuffix(tr(" pixels"));
131     gl4->addWidget(boxRightSpace, 3, 1);
132     gl4->addWidget(new QLabel(tr("Top margin")), 4, 0);
133     boxTopSpace = new QSpinBox();
134     boxTopSpace->setRange(0, 1000);
135     boxTopSpace->setSingleStep(5);
136     boxTopSpace->setSuffix(tr(" pixels"));
137     gl4->addWidget(boxTopSpace, 4, 1);
138     gl4->addWidget(new QLabel(tr("Bottom margin")), 5, 0);
139     boxBottomSpace = new QSpinBox();
140     boxBottomSpace->setRange(0, 1000);
141     boxBottomSpace->setSingleStep(5);
142     boxBottomSpace->setSuffix(tr(" pixels"));
143     gl4->addWidget(boxBottomSpace, 5, 1);
144 
145     QVBoxLayout *vbox1 = new QVBoxLayout();
146     vbox1->addWidget(GroupGrid);
147     vbox1->addWidget(GroupCanvasSize);
148 
149     buttonApply = new QPushButton(tr("&Apply"));
150     buttonOk = new QPushButton(tr("&OK"));
151     buttonCancel = new QPushButton(tr("&Cancel"));
152 
153     QHBoxLayout *hbox1 = new QHBoxLayout();
154     hbox1->addStretch();
155     hbox1->addWidget(buttonApply);
156     hbox1->addWidget(buttonOk);
157     hbox1->addWidget(buttonCancel);
158 
159     QGridLayout *gl6 = new QGridLayout();
160     gl6->addWidget(gb1, 0, 0);
161     gl6->addWidget(gb2, 0, 1);
162     gl6->addLayout(vbox1, 1, 0);
163     gl6->addWidget(gb4, 1, 1);
164     gl6->setRowStretch(2, 1);
165 
166     QVBoxLayout *vbox2 = new QVBoxLayout(this);
167     vbox2->addLayout(gl6);
168     vbox2->addLayout(hbox1);
169 
170     connect(buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
171     connect(buttonApply, SIGNAL(clicked()), this, SLOT(update()));
172     connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
173     connect(fitBox, SIGNAL(toggled(bool)), this, SLOT(enableLayoutOptions(bool)));
174 }
175 
enableLayoutOptions(bool ok)176 void LayerDialog::enableLayoutOptions(bool ok)
177 {
178     GroupGrid->setEnabled(!ok);
179     GroupCanvasSize->setEnabled(!ok);
180 }
181 
setMultiLayer(MultiLayer * g)182 void LayerDialog::setMultiLayer(MultiLayer *g)
183 {
184     multi_layer = g;
185 
186     layersBox->setValue(g->layers());
187     boxX->setValue(g->getCols());
188     boxY->setValue(g->getRows());
189     boxColsGap->setValue(g->colsSpacing());
190     boxRowsGap->setValue(g->rowsSpacing());
191     boxLeftSpace->setValue(g->leftMargin());
192     boxRightSpace->setValue(g->rightMargin());
193     boxTopSpace->setValue(g->topMargin());
194     boxBottomSpace->setValue(g->bottomMargin());
195     boxCanvasWidth->setValue(g->layerCanvasSize().width());
196     boxCanvasHeight->setValue(g->layerCanvasSize().height());
197     alignHorBox->setCurrentIndex(g->horizontalAlignement());
198     alignVertBox->setCurrentIndex(g->verticalAlignement());
199 }
200 
update()201 void LayerDialog::update()
202 {
203     int graphs = layersBox->value();
204     int old_graphs = multi_layer->layers();
205     int dn = multi_layer->layers() - graphs;
206     if (dn > 0
207         && QMessageBox::question(0, tr("Delete Layers?"),
208                                  tr("You are about to delete %1 existing layers.").arg(dn) + "\n"
209                                          + tr("Are you sure you want to continue this operation?"),
210                                  tr("&Continue"), tr("&Cancel"), QString(), 0, 1))
211         return;
212 
213     multi_layer->setLayersNumber(graphs);
214 
215     if (!graphs)
216         return;
217 
218     if (dn < 0) { // Customize new layers with user default settings
219         ApplicationWindow *app = (ApplicationWindow *)this->parent();
220         for (int i = old_graphs + 1; i <= graphs; i++)
221             app->setPreferences(multi_layer->layer(i));
222     }
223 
224     int cols = boxX->value();
225     int rows = boxY->value();
226 
227     if (cols > graphs && !fitBox->isChecked()) {
228         QMessageBox::about(0, tr("Columns input error"),
229                            tr("The number of columns you've entered is greater than the number of "
230                               "graphs (%1)!")
231                                    .arg(graphs));
232         boxX->setFocus();
233         return;
234     }
235 
236     if (rows > graphs && !fitBox->isChecked()) {
237         QMessageBox::about(
238                 0, tr("Rows input error"),
239                 tr("The number of rows you've entered is greater than the number of graphs (%1)!")
240                         .arg(graphs));
241         boxY->setFocus();
242         return;
243     }
244 
245     if (!fitBox->isChecked()) {
246         multi_layer->setCols(cols);
247         multi_layer->setRows(rows);
248     }
249 
250     if (GroupCanvasSize->isChecked())
251         multi_layer->setLayerCanvasSize(boxCanvasWidth->value(), boxCanvasHeight->value());
252 
253     multi_layer->setAlignement(alignHorBox->currentIndex(), alignVertBox->currentIndex());
254 
255     multi_layer->setMargins(boxLeftSpace->value(), boxRightSpace->value(), boxTopSpace->value(),
256                             boxBottomSpace->value());
257 
258     multi_layer->setSpacing(boxRowsGap->value(), boxColsGap->value());
259     multi_layer->arrangeLayers(fitBox->isChecked(), GroupCanvasSize->isChecked());
260 
261     if (!GroupCanvasSize->isChecked()) { // show new layer canvas size
262         boxCanvasWidth->setValue(multi_layer->layerCanvasSize().width());
263         boxCanvasHeight->setValue(multi_layer->layerCanvasSize().height());
264     }
265 
266     if (fitBox->isChecked()) { // show new grid settings
267         boxX->setValue(multi_layer->getCols());
268         boxY->setValue(multi_layer->getRows());
269     }
270 }
271 
accept()272 void LayerDialog::accept()
273 {
274     update();
275     close();
276 }
277