/
KSegSelectionGroupDialog.cpp
309 lines (240 loc) · 8.4 KB
/
KSegSelectionGroupDialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* KSeg
* Copyright (C) 1999-2006 Ilya Baran
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Send comments and/or bug reports to:
* ibaran@mit.edu
*/
#include <iostream>
#include <qtooltip.h>
#include <qlineedit.h>
#include <qmessagebox.h>
#include <QHBoxLayout>
#include <QShowEvent>
#include <QLabel>
#include <QVBoxLayout>
#include "KSegView.H"
#include "KSegDocument.H"
#include "KSegSelectionGroupDialog.H"
#include "KSegSelectionGroup.H"
KSegSelectionGroupDialog::KSegSelectionGroupDialog(KSegView *view,
vector<KSegSelectionGroup *> &inGroups,
KSegDocument *inDoc)
: QDialog(view), justMade(true), doc(inDoc), groups(inGroups)
{
QHBoxLayout *hbl = new QHBoxLayout(this);
groupLBox = new QListWidget(this);
groupLBox->setMinimumSize(140, 300);
hbl->addWidget(groupLBox);
hbl->addSpacing(8);
QVBoxLayout *vbl = new QVBoxLayout();
selectBut = new QPushButton(tr("Select"), this);
deselectBut = new QPushButton(tr("Deselect"), this);
newBut = new QPushButton(tr("New Group"), this);
deleteBut = new QPushButton(tr("Delete Group"),this);
closeBut = new QPushButton(tr("Close"), this);
affectInvisible = new QCheckBox(tr("Affect\nInvisible\nItems"), this);
QToolTip::add(selectBut, tr("Hold SHIFT and click to add to current selection"));
selectBut->installEventFilter(this);
QToolTip::add(affectInvisible, tr("Whether the \"All <Type>\" groups also (de)select invisible objects"));
vbl->addSpacing(8);
vbl->addWidget(selectBut);
vbl->addWidget(deselectBut);
vbl->addWidget(newBut);
vbl->addWidget(deleteBut);
vbl->addWidget(closeBut);
vbl->addSpacing(16);
vbl->addWidget(affectInvisible);
vbl->addItem(new QSpacerItem(0, 0));
hbl->addLayout(vbl);
hbl->addSpacing(8);
//now set up items properties
connect(groupLBox, SIGNAL(itemSelectionChanged()), this, SLOT(updateButtons()));
connect(doc, SIGNAL(documentChanged()), this, SLOT(updateButtons()));
connect(selectBut, SIGNAL(clicked()), this, SLOT(select()));
connect(deselectBut, SIGNAL(clicked()), this, SLOT(deselect()));
connect(newBut, SIGNAL(clicked()), this, SLOT(newGroup()));
connect(deleteBut, SIGNAL(clicked()), this, SLOT(deleteGroup()));
connect(closeBut, SIGNAL(clicked()), this, SLOT(close()));
groupLBox->setSelectionMode(QAbstractItemView::ExtendedSelection);
groupLBox->addItem("*" + tr("All Points"));
groupLBox->addItem("*" + tr("All Segments"));
groupLBox->addItem("*" + tr("All Rays"));
groupLBox->addItem("*" + tr("All Lines"));
groupLBox->addItem("*" + tr("All Circles"));
groupLBox->addItem("*" + tr("All Arcs"));
groupLBox->addItem("*" + tr("All Polygons"));
groupLBox->addItem("*" + tr("All Circle Interiors"));
groupLBox->addItem("*" + tr("All Arc Sectors"));
groupLBox->addItem("*" + tr("All Arc Segments"));
groupLBox->addItem("*" + tr("All Loci"));
groupLBox->addItem("*" + tr("All Measurements"));
groupLBox->addItem("*" + tr("All Calculations"));
numFixedGroups = groupLBox->count();
int i;
for(i = 0; i < (int)groups.size(); ++i) {
groupLBox->addItem(" " + groups[i]->getName());
}
}
void KSegSelectionGroupDialog::showEvent(QShowEvent *)
{
QWidget *top = ((QWidget *)parent())->topLevelWidget();
if(justMade) move(max(0, top->x() - frameGeometry().width() - 15), top->y());
justMade = false;
updateButtons();
}
void KSegSelectionGroupDialog::updateButtons()
{
int i;
bool anySelected = false;
bool onlyNonFixedSelected = false;
for(i = 0; i < (int)groupLBox->count(); ++i) {
if(!groupLBox->item(i)->isSelected()) continue;
if(i >= numFixedGroups && !anySelected) {
anySelected = true;
onlyNonFixedSelected = true;
break;
}
anySelected = true;
}
selectBut->setEnabled(anySelected);
deselectBut->setEnabled(anySelected);
deleteBut->setEnabled(onlyNonFixedSelected);
newBut->setEnabled(doc->getSelected().size() > 0);
for(i = numFixedGroups; i < (int)groupLBox->count(); ++i) {
QString s = groupLBox->item(i)->text();
if(groups[i - numFixedGroups]->size() == 0) { //empty
if(s[0] != '-') {
s[0] = '-';
groupLBox->item(i)->setText(s);
}
}
else if(s[0] != ' ') {
s[0] = ' ';
groupLBox->item(i)->setText(s);
}
}
}
void KSegSelectionGroupDialog::select()
{
int i;
int filter = 0;
bool alsoInvisible = affectInvisible->isChecked();
for(i = 0; i < numFixedGroups; ++i) {
if(groupLBox->item(i)->isSelected()) {
filter |= (1 << i);
}
}
if(!shiftDown) doc->clearSel();
for(i = 0; i < doc->getNumRefs(); ++i) {
if(doc->getRef(i)->getType() & filter) {
if(!alsoInvisible && !doc->getRef(i)->getVisible()) continue;
doc->addSel(doc->getRef(i));
}
}
for(i = numFixedGroups; i < (int)groupLBox->count(); ++i) {
if(groupLBox->item(i)->isSelected()) {
groups[i - numFixedGroups]->selectRefs();
}
}
doc->emitDocumentChanged();
((QWidget *)parent())->topLevelWidget()->setActiveWindow();
}
void KSegSelectionGroupDialog::deselect()
{
int i;
int filter = 0;
bool alsoInvisible = affectInvisible->isChecked();
for(i = 0; i < numFixedGroups; ++i) {
if(groupLBox->item(i)->isSelected()) {
filter |= (1 << i);
}
}
for(i = 0; i < doc->getNumRefs(); ++i) {
if(doc->getRef(i)->getType() & filter) {
if(!alsoInvisible && !doc->getRef(i)->getVisible()) continue;
doc->delSel(doc->getRef(i));
}
}
for(i = numFixedGroups; i < (int)groupLBox->count(); ++i) {
if(groupLBox->item(i)->isSelected()) {
groups[i - numFixedGroups]->deselectRefs();
}
}
doc->emitDocumentChanged();
((QWidget *)parent())->topLevelWidget()->setActiveWindow();
}
void KSegSelectionGroupDialog::newGroup()
{
if(doc->getSelected().size() == 0) return; //shouldn't be here anyway
//set up the name query dialog
QDialog nameDlg(0, 0, true);
QPushButton *ok, *cancel;
QLabel *query;
QLineEdit *edit;
QVBoxLayout vlayout(&nameDlg);
vlayout.addSpacing(8);
QHBoxLayout hlayout0;
vlayout.addLayout(&hlayout0);
hlayout0.addSpacing(8);
query = new QLabel(tr("Enter new group name:"), &nameDlg);
hlayout0.addWidget(query);
hlayout0.addSpacing(8);
edit = new QLineEdit(&nameDlg);
hlayout0.addWidget(edit);
hlayout0.addSpacing(8);
vlayout.addSpacing(8);
QHBoxLayout hlayout;
vlayout.addLayout(&hlayout);
hlayout.addSpacing(8);
ok = new QPushButton(qApp->translate("KSegDocument", "OK", ""), &nameDlg );
hlayout.addWidget(ok);
QObject::connect( ok, SIGNAL(clicked()), &nameDlg, SLOT(accept()) );
ok->setDefault(true);
hlayout.addSpacing(8);
cancel = new QPushButton(qApp->translate("KSegDocument", "Cancel", ""), &nameDlg );
hlayout.addWidget(cancel);
QObject::connect( cancel, SIGNAL(clicked()), &nameDlg, SLOT(reject()) );
hlayout.addSpacing(8);
vlayout.addSpacing(8);
//run the dialog
nameDlg.exec();
if(nameDlg.result() == QDialog::Rejected) return;
//create the group
groups.push_back(new KSegSelectionGroup(doc));
groups.back()->setName(edit->text());
for(int i = 0; i < (int)doc->getSelected().size(); ++i)
groups.back()->addRef(doc->getSelected()[i]);
groupLBox->addItem(" " + edit->text());
doc->emitDocumentModified();
}
void KSegSelectionGroupDialog::deleteGroup()
{
int res = QMessageBox::information(this, "kseg", tr("Are you sure you want to delete the selected group(s)?"),
QMessageBox::Yes, QMessageBox::No);
if(res == QMessageBox::No) return;
int i;
for(i = 0; i < (int)groupLBox->count(); ++i) {
if(!groupLBox->item(i)->isSelected()) continue;
if(i < numFixedGroups) continue; //this really shouldn't happen, but...
groups.erase(groups.begin() + (i - numFixedGroups));
//groupLBox->removeItemWidget(groupLBox->item(i));
groupLBox->model()->removeRow(i);
i--;
}
doc->emitDocumentModified();
}