1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
9 ** This file may be distributed and/or modified under the terms of the
10 ** GNU General Public License version 2 as published by the Free Software
11 ** Foundation and appearing in the file gpl-2.0.txt included in the
12 ** packaging of this file.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26
27 #include "qg_patternbox.h"
28
29 #include <QPixmap>
30 #include <QStringList>
31
32 #include "rs_pattern.h"
33 #include "rs_patternlist.h"
34 #include "rs_debug.h"
35
36
37 /**
38 * Default Constructor. You must call init manually if you choose
39 * to use this constructor.
40 */
QG_PatternBox(QWidget * parent)41 QG_PatternBox::QG_PatternBox(QWidget* parent)
42 : QComboBox(parent) {
43 }
44
45 /**
46 * Destructor
47 */
48 QG_PatternBox::~QG_PatternBox() = default;
49
50
51 /**
52 * Initialisation (called manually and only once).
53 */
init()54 void QG_PatternBox::init() {
55 QStringList patterns;
56
57 for (auto const& pa: *RS_PATTERNLIST)
58 patterns.append(pa.first);
59
60 patterns.sort();
61 insertItems(0, patterns);
62
63 connect(this, SIGNAL(activated(int)),
64 this, SLOT(slotPatternChanged(int)));
65
66 setCurrentIndex(0);
67 slotPatternChanged(currentIndex());
68 }
69
70
71
72 /**
73 * Sets the currently selected width item to the given width.
74 */
setPattern(const QString & pName)75 void QG_PatternBox::setPattern(const QString& pName) {
76
77 RS_DEBUG->print("QG_PatternBox::setPattern %s\n", pName.toLatin1().data());
78
79 setCurrentIndex(findText(pName));
80
81 slotPatternChanged(currentIndex());
82 }
83
getPattern()84 RS_Pattern* QG_PatternBox::getPattern() {
85 if (currentPattern == nullptr || currentPattern->countDeep()==0)
86 currentPattern = RS_PATTERNLIST->requestPattern(currentText());
87 return currentPattern;
88 }
89
90 /**
91 * Called when the pattern has changed. This method
92 * sets the current pattern to the value chosen.
93 */
slotPatternChanged(int index)94 void QG_PatternBox::slotPatternChanged(int index) {
95
96 RS_DEBUG->print("QG_PatternBox::slotPatternChanged %d\n", index);
97
98 currentPattern = RS_PATTERNLIST->requestPattern(currentText());
99
100 if (currentPattern) {
101 RS_DEBUG->print("Current pattern is (%d): %s\n",
102 index, currentPattern->getFileName().toLatin1().data());
103 }
104
105
106 emit patternChanged();
107 }
108
109
110