radiobox.cpp

Tue, 07 May 2013 19:48:51 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Tue, 07 May 2013 19:48:51 +0300
changeset 175
b094d5e9d6e0
parent 174
963697b36118
permissions
-rw-r--r--

Added Intersector interface. I'm beginning to think that groups were a bad idea...

174
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2013 Santeri Piippo
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include <qboxlayout.h>
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include <qradiobutton.h>
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 #include "radiobox.h"
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 static QBoxLayout::Direction makeDirection (Qt::Orientation orient, bool invert = false) {
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 return (orient == (invert ? Qt::Vertical : Qt::Horizontal)) ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom;
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 }
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 void RadioBox::init (Qt::Orientation orient) {
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 dir = makeDirection (orient);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 buttonGroup = new QButtonGroup;
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 currentId = 0;
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 coreLayout = null;
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 coreLayout = new QBoxLayout (makeDirection (orient, true));
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 setLayout (coreLayout);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 // Init the first row with a break
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 rowBreak ();
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 connect (buttonGroup, SIGNAL (buttonPressed (QAbstractButton*)), this, SLOT (slot_buttonPressed (QAbstractButton*)));
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 connect (buttonGroup, SIGNAL (buttonPressed (int)), this, SLOT (slot_buttonPressed (int)));
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 }
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 RadioBox::RadioBox (const QString& title, initlist<char const*> entries, int const defaultId,
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 const Qt::Orientation orient, QWidget* parent) : QGroupBox (title, parent), defaultId (defaultId)
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 {
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 init (orient);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 for (char const* entry : entries)
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 addButton (entry);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 }
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 void RadioBox::rowBreak () {
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 QBoxLayout* newLayout = new QBoxLayout (dir);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 currentLayout = newLayout;
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 layouts.push_back (newLayout);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 coreLayout->addLayout (newLayout);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 }
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 void RadioBox::addButton (const char* entry) {
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 QRadioButton* button = new QRadioButton (entry);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 addButton (button);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 }
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 void RadioBox::addButton (QRadioButton* button) {
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 bool const selectThis = (currentId == defaultId);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 objects.push_back (button);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 buttonGroup->addButton (button, currentId++);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 currentLayout->addWidget (button);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 if (selectThis)
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 button->setChecked (true);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 }
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 RadioBox& RadioBox::operator<< (QRadioButton* button) {
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 addButton (button);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 return *this;
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 }
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 RadioBox& RadioBox::operator<< (const char* entry) {
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 addButton (entry);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 return *this;
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 }
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 void RadioBox::setCurrentRow (uint row) {
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 currentLayout = layouts[row];
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 }
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 void RadioBox::slot_buttonPressed (int btn) {
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 emit sig_buttonPressed (btn);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 }
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 void RadioBox::slot_buttonPressed (QAbstractButton* btn) {
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 emit sig_buttonPressed (btn);
963697b36118 Added rectifier interface
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 }

mercurial