labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

GuiTools.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  File : GuiTools.cpp
3  Project : LabPlot
4  --------------------------------------------------------------------
5  Copyright : (C) 2011-2013 Alexander Semke (alexander.semke*web.de)
6  (replace * with @ in the email addresses)
7  Description : contains several static functions which are used on frequently throughout the kde frontend.
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 
30 #include "GuiTools.h"
31 #include <KI18n/KLocalizedString>
32 
33 #include <array>
34 #include <QApplication>
35 #include <QComboBox>
36 #include <QLineEdit>
37 #include <QMenu>
38 #include <QColor>
39 #include <QPainter>
40 
41 static const int colorsCount = 26;
42 static std::array<QColor, colorsCount> colors = {QColor(255,255,255), QColor(0,0,0),
43  QColor(192,0,0), QColor(255,0,0), QColor(255,192,192), //red
44  QColor(0,192,0), QColor(0,255,0), QColor(192,255,192), //green
45  QColor(0,0,192), QColor(0,0,255), QColor(192,192,255), //blue
46  QColor(192,192,0), QColor(255,255,0), QColor(255,255,192), //yellow
47  QColor(0,192,192), QColor(0,255,255), QColor(192,255,255), //cyan
48  QColor(192,0,192), QColor(255,0,255), QColor(255,192,255), //magenta
49  QColor(192,88,0), QColor(255,128,0), QColor(255,168,88), //orange
50  QColor(128,128,128), QColor(160,160,160), QColor(195,195,195) //grey
51  };
52 
53 /*!
54  fills the ComboBox \c combobox with the six possible Qt::PenStyles, the color \c color is used.
55 */
56 void GuiTools::updatePenStyles(QComboBox* comboBox, const QColor& color) {
57  int index = comboBox->currentIndex();
58  comboBox->clear();
59 
60  QPainter pa;
61  int offset = 2;
62  int w = 50;
63  int h = 10;
64  QPixmap pm(w, h);
65  comboBox->setIconSize(QSize(w,h));
66 
67  //loop over six possible Qt-PenStyles, draw on the pixmap and insert it
68  //TODO: avoid copy-paste in all finctions!
69  static std::array<QString, 6> list = { i18n("No Line"), i18n("Solid Line"), i18n("Dash Line"),
70  i18n("Dot Line"), i18n("Dash-dot Line"), i18n("Dash-dot-dot Line") };
71  for (int i = 0; i < 6; i++) {
72  pm.fill(Qt::transparent);
73  pa.begin(&pm);
74  pa.setPen( QPen(color, 1, (Qt::PenStyle)i) );
75  pa.drawLine(offset, h/2, w-offset, h/2);
76  pa.end();
77  comboBox->addItem( QIcon(pm), list[i] );
78  }
79  comboBox->setCurrentIndex(index);
80 }
81 
82 /*!
83  fills the QMenu \c menu with the six possible Qt::PenStyles, the color \c color is used.
84  QActions are created with \c actionGroup as the parent, if not available.
85  If already available, onle the color in the QAction's icons is updated.
86 */
87 void GuiTools::updatePenStyles(QMenu* menu, QActionGroup* actionGroup, const QColor& color) {
88  QPainter pa;
89  int offset = 2;
90  int w = 50;
91  int h = 10;
92  QPixmap pm(w, h);
93 
94  //loop over six possible Qt-PenStyles, draw on the pixmap and insert it
95  static std::array<QString, 6> list = { i18n("No Line"), i18n("Solid Line"), i18n("Dash Line"),
96  i18n("Dot Line"), i18n("Dash-dot Line"), i18n("Dash-dot-dot Line") };
97 
98  QAction* action;
99  if (actionGroup->actions().isEmpty()) {
100  //TODO setting of the icon size doesn't work here
101  menu->setStyleSheet( "QMenu::icon { width:50px; height:10px; }" );
102 
103  for (int i = 0; i < 6; i++) {
104  pm.fill(Qt::transparent);
105  pa.begin(&pm);
106  pa.setPen( QPen( color, 1, (Qt::PenStyle)i ) );
107  pa.drawLine(offset, h/2, w-offset, h/2);
108  pa.end();
109  action = new QAction( QIcon(pm), list[i], actionGroup );
110  action->setCheckable(true);
111  menu->addAction( action );
112  }
113  } else {
114  for (int i = 0; i < 6; i++) {
115  pm.fill(Qt::transparent);
116  pa.begin( &pm );
117  pa.setPen( QPen( color, 1, (Qt::PenStyle)i ) );
118  pa.drawLine(offset, h/2, w-offset, h/2);
119  pa.end();
120  action = actionGroup->actions().at(i);
121  action->setIcon( QIcon(pm) );
122  }
123  }
124 }
125 
126 void GuiTools::selectPenStyleAction(QActionGroup* actionGroup, Qt::PenStyle style) {
127  int index = (int)style;
128  Q_ASSERT(index < actionGroup->actions().size());
129  actionGroup->actions().at(index)->setChecked(true);
130 }
131 
132 Qt::PenStyle GuiTools::penStyleFromAction(QActionGroup* actionGroup, QAction* action) {
133  int index = actionGroup->actions().indexOf(action);
134  return Qt::PenStyle(index);
135 }
136 
137 /*!
138  fills the ComboBox for the symbol filling patterns with the 14 possible Qt::BrushStyles.
139 */
140 void GuiTools::updateBrushStyles(QComboBox* comboBox, const QColor& color) {
141  int index = comboBox->currentIndex();
142  comboBox->clear();
143 
144  QPainter pa;
145  int offset = 2;
146  int w = 50;
147  int h = 20;
148  QPixmap pm(w, h);
149  comboBox->setIconSize(QSize(w, h));
150 
151  QPen pen(Qt::SolidPattern, 1);
152  pa.setPen(pen);
153 
154  static std::array<QString, 15> list = { i18n("None"), i18n("Uniform"), i18n("Extremely Dense"),
155  i18n("Very Dense"), i18n("Somewhat Dense"), i18n("Half Dense"),
156  i18n("Somewhat Sparse"), i18n("Very Sparse"), i18n("Extremely Sparse"),
157  i18n("Horiz. Lines"), i18n("Vert. Lines"), i18n("Crossing Lines"),
158  i18n("Backward Diag. Lines"), i18n("Forward Diag. Lines"), i18n("Crossing Diag. Lines") };
159  const QColor& borderColor = (qApp->palette().color(QPalette::Base).lightness() < 128) ? Qt::white : Qt::black;
160  for (int i = 0; i < 15; i++) {
161  pm.fill(Qt::transparent);
162  pa.begin(&pm);
163  pa.setPen(borderColor);
164  pa.setRenderHint(QPainter::Antialiasing);
165  pa.setBrush( QBrush(color, (Qt::BrushStyle)i) );
166  pa.drawRect(offset, offset, w - 2*offset, h - 2*offset);
167  pa.end();
168  comboBox->addItem(QIcon(pm), list[i]);
169  }
170 
171  comboBox->setCurrentIndex(index);
172 }
173 
174 void GuiTools::fillColorMenu(QMenu* menu, QActionGroup* actionGroup) {
175  static const std::array<QString, colorsCount> colorNames = {i18n("White"), i18n("Black"),
176  i18n("Dark Red"), i18n("Red"), i18n("Light Red"),
177  i18n("Dark Green"), i18n("Green"), i18n("Light Green"),
178  i18n("Dark Blue"), i18n("Blue"), i18n("Light Blue"),
179  i18n("Dark Yellow"), i18n("Yellow"), i18n("Light Yellow"),
180  i18n("Dark Cyan"), i18n("Cyan"), i18n("Light Cyan"),
181  i18n("Dark Magenta"), i18n("Magenta"), i18n("Light Magenta"),
182  i18n("Dark Orange"), i18n("Orange"), i18n("Light Orange"),
183  i18n("Dark Grey"), i18n("Grey"), i18n("Light Grey")
184  };
185 
186  QPixmap pix(16, 16);
187  QPainter p(&pix);
188  for (int i = 0; i < colorsCount; ++i) {
189  p.fillRect(pix.rect(), colors[i]);
190  QAction* action = new QAction(QIcon(pix), colorNames[i], actionGroup);
191  action->setCheckable(true);
192  menu->addAction(action);
193  }
194 }
195 
196 /*!
197  * Selects (checks) the action in the group \c actionGroup hat corresponds to the color \c color.
198  * Unchecks the previously checked action if the color
199  * was not found in the list of predefined colors.
200  */
201 void GuiTools::selectColorAction(QActionGroup* actionGroup, const QColor& color) {
202  int index;
203  for (index = 0; index < colorsCount; ++index) {
204  if (color == colors[index]) {
205  actionGroup->actions().at(index)->setChecked(true);
206  break;
207  }
208  }
209 
210  if (index == colorsCount) {
211  //the color was not found in the list of predefined colors
212  // -> uncheck the previously checked action
213  QAction* checkedAction = actionGroup->checkedAction();
214  if (checkedAction)
215  checkedAction->setChecked(false);
216  }
217 }
218 
219 QColor& GuiTools::colorFromAction(QActionGroup* actionGroup, QAction* action) {
220  int index = actionGroup->actions().indexOf(action);
221  if (index == -1 || index >= colorsCount)
222  index = 0;
223 
224  return colors[index];
225 }
226 
227 // ComboBox with colors
228 // QImage img(16,16,QImage::Format_RGB32);
229 // QPainter p(&img);
230 // QRect rect = img.rect().adjusted(1,1,-1,-1);
231 // p.fillRect(rect, Qt::red);
232 // comboBox->setItemData(0, QPixmap::fromImage(img), Qt::DecorationRole);
233 
234 void GuiTools::highlight(QLineEdit* le, bool invalid) {
235  if (invalid)
236  le->setStyleSheet(QStringLiteral("QLineEdit{color:red;}"));
237  else
238  le->setStyleSheet(QString());
239 }
static std::array< QColor, colorsCount > colors
Definition: GuiTools.cpp:42
static const int colorsCount
Definition: GuiTools.cpp:41
static const QRgb white
Definition: ImageEditor.cpp:37
static const QRgb black
Definition: ImageEditor.cpp:38
static void updateBrushStyles(QComboBox *, const QColor &)
Definition: GuiTools.cpp:140
static void updatePenStyles(QComboBox *, const QColor &)
Definition: GuiTools.cpp:56
static void highlight(QLineEdit *, bool)
Definition: GuiTools.cpp:234
static void fillColorMenu(QMenu *, QActionGroup *)
Definition: GuiTools.cpp:174
static QColor & colorFromAction(QActionGroup *, QAction *)
Definition: GuiTools.cpp:219
static Qt::PenStyle penStyleFromAction(QActionGroup *, QAction *)
Definition: GuiTools.cpp:132
static void selectPenStyleAction(QActionGroup *, Qt::PenStyle)
Definition: GuiTools.cpp:126
static void selectColorAction(QActionGroup *, const QColor &)
Definition: GuiTools.cpp:201
#define i18n(m)
Definition: nsl_common.h:38