1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2  * Qwt Widget Library
3  * Copyright (C) 1997   Josef Wilgen
4  * Copyright (C) 2002   Uwe Rathmann
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the Qwt License, Version 1.0
8  *****************************************************************************/
9 
10 #if defined(_MSC_VER) /* MSVC Compiler */
11 #pragma warning ( disable : 4786 )
12 #endif
13 
14 #include <qglobal.h>
15 #include <qaction.h>
16 #include <QtPlugin>
17 #include <QDesignerFormEditorInterface>
18 #include <QDesignerFormWindowInterface>
19 #include <QDesignerFormWindowCursorInterface>
20 #include <QExtensionManager>
21 #include <QErrorMessage>
22 
23 #include "qwt_designer_plugin.h"
24 
25 #ifndef NO_QWT_PLOT
26 #include "qwt_designer_plotdialog.h"
27 #include "qwt_plot.h"
28 #include "qwt_scale_widget.h"
29 #endif
30 
31 #ifndef NO_QWT_WIDGETS
32 #include "qwt_counter.h"
33 #include "qwt_wheel.h"
34 #include "qwt_thermo.h"
35 #include "qwt_knob.h"
36 #include "qwt_slider.h"
37 #include "qwt_analog_clock.h"
38 #include "qwt_compass.h"
39 #endif
40 
41 #include "qwt_text_label.h"
42 
43 using namespace QwtDesignerPlugin;
44 
CustomWidgetInterface(QObject * parent)45 CustomWidgetInterface::CustomWidgetInterface(QObject *parent):
46     QObject(parent),
47     d_isInitialized(false)
48 {
49 }
50 
isContainer() const51 bool CustomWidgetInterface::isContainer() const
52 {
53     return false;
54 }
55 
isInitialized() const56 bool CustomWidgetInterface::isInitialized() const
57 {
58     return d_isInitialized;
59 }
60 
icon() const61 QIcon CustomWidgetInterface::icon() const
62 {
63     return d_icon;
64 }
65 
codeTemplate() const66 QString CustomWidgetInterface::codeTemplate() const
67 {
68     return d_codeTemplate;
69 }
70 
domXml() const71 QString CustomWidgetInterface::domXml() const
72 {
73     return d_domXml;
74 }
75 
group() const76 QString CustomWidgetInterface::group() const
77 {
78     return "Qwt Widgets";
79 }
80 
includeFile() const81 QString CustomWidgetInterface::includeFile() const
82 {
83     return d_include;
84 }
85 
name() const86 QString CustomWidgetInterface::name() const
87 {
88     return d_name;
89 }
90 
toolTip() const91 QString CustomWidgetInterface::toolTip() const
92 {
93     return d_toolTip;
94 }
95 
whatsThis() const96 QString CustomWidgetInterface::whatsThis() const
97 {
98     return d_whatsThis;
99 }
100 
initialize(QDesignerFormEditorInterface * formEditor)101 void CustomWidgetInterface::initialize(
102     QDesignerFormEditorInterface *formEditor)
103 {
104     if ( d_isInitialized )
105         return;
106 
107     QExtensionManager *manager = formEditor->extensionManager();
108     if ( manager )
109     {
110         manager->registerExtensions(new TaskMenuFactory(manager),
111             Q_TYPEID(QDesignerTaskMenuExtension));
112     }
113 
114     d_isInitialized = true;
115 }
116 
117 #ifndef NO_QWT_PLOT
118 
PlotInterface(QObject * parent)119 PlotInterface::PlotInterface(QObject *parent):
120     CustomWidgetInterface(parent)
121 {
122     d_name = "QwtPlot";
123     d_include = "qwt_plot.h";
124     d_icon = QPixmap(":/pixmaps/qwtplot.png");
125     d_domXml =
126         "<widget class=\"QwtPlot\" name=\"qwtPlot\">\n"
127         " <property name=\"geometry\">\n"
128         "  <rect>\n"
129         "   <x>0</x>\n"
130         "   <y>0</y>\n"
131         "   <width>400</width>\n"
132         "   <height>200</height>\n"
133         "  </rect>\n"
134         " </property>\n"
135         "</widget>\n";
136 }
137 
createWidget(QWidget * parent)138 QWidget *PlotInterface::createWidget(QWidget *parent)
139 {
140     return new QwtPlot(parent);
141 }
142 
143 #endif
144 
145 #ifndef NO_QWT_WIDGETS
146 
AnalogClockInterface(QObject * parent)147 AnalogClockInterface::AnalogClockInterface(QObject *parent):
148     CustomWidgetInterface(parent)
149 {
150     d_name = "QwtAnalogClock";
151     d_include = "qwt_analog_clock.h";
152     d_icon = QPixmap(":/pixmaps/qwtanalogclock.png");
153     d_domXml =
154         "<widget class=\"QwtAnalogClock\" name=\"AnalogClock\">\n"
155         " <property name=\"geometry\">\n"
156         "  <rect>\n"
157         "   <x>0</x>\n"
158         "   <y>0</y>\n"
159         "   <width>200</width>\n"
160         "   <height>200</height>\n"
161         "  </rect>\n"
162         " </property>\n"
163         " <property name=\"lineWidth\">\n"
164         "  <number>4</number>\n"
165         " </property>\n"
166         "</widget>\n";
167 }
168 
createWidget(QWidget * parent)169 QWidget *AnalogClockInterface::createWidget(QWidget *parent)
170 {
171     return new QwtAnalogClock(parent);
172 }
173 
174 #endif
175 
176 #ifndef NO_QWT_WIDGETS
177 
CompassInterface(QObject * parent)178 CompassInterface::CompassInterface(QObject *parent):
179     CustomWidgetInterface(parent)
180 {
181     d_name = "QwtCompass";
182     d_include = "qwt_compass.h";
183     d_icon = QPixmap(":/pixmaps/qwtcompass.png");
184     d_domXml =
185         "<widget class=\"QwtCompass\" name=\"Compass\">\n"
186         " <property name=\"geometry\">\n"
187         "  <rect>\n"
188         "   <x>0</x>\n"
189         "   <y>0</y>\n"
190         "   <width>200</width>\n"
191         "   <height>200</height>\n"
192         "  </rect>\n"
193         " </property>\n"
194         " <property name=\"lineWidth\">\n"
195         "  <number>4</number>\n"
196         " </property>\n"
197         "</widget>\n";
198 }
199 
createWidget(QWidget * parent)200 QWidget *CompassInterface::createWidget(QWidget *parent)
201 {
202     return new QwtCompass(parent);
203 }
204 
205 #endif
206 
207 #ifndef NO_QWT_WIDGETS
208 
CounterInterface(QObject * parent)209 CounterInterface::CounterInterface(QObject *parent):
210     CustomWidgetInterface(parent)
211 {
212     d_name = "QwtCounter";
213     d_include = "qwt_counter.h";
214     d_icon = QPixmap(":/pixmaps/qwtcounter.png");
215     d_domXml =
216         "<widget class=\"QwtCounter\" name=\"Counter\">\n"
217         "</widget>\n";
218 }
219 
createWidget(QWidget * parent)220 QWidget *CounterInterface::createWidget(QWidget *parent)
221 {
222     return new QwtCounter(parent);
223 }
224 
225 #endif
226 
227 #ifndef NO_QWT_WIDGETS
228 
DialInterface(QObject * parent)229 DialInterface::DialInterface(QObject *parent):
230     CustomWidgetInterface(parent)
231 {
232     d_name = "QwtDial";
233     d_include = "qwt_dial.h";
234     d_icon = QPixmap(":/pixmaps/qwtdial.png");
235     d_domXml =
236         "<widget class=\"QwtDial\" name=\"Dial\">\n"
237         " <property name=\"geometry\">\n"
238         "  <rect>\n"
239         "   <x>0</x>\n"
240         "   <y>0</y>\n"
241         "   <width>200</width>\n"
242         "   <height>200</height>\n"
243         "  </rect>\n"
244         " </property>\n"
245         " <property name=\"lineWidth\">\n"
246         "  <number>4</number>\n"
247         " </property>\n"
248         "</widget>\n";
249 }
250 
createWidget(QWidget * parent)251 QWidget *DialInterface::createWidget(QWidget *parent)
252 {
253     return new QwtDial(parent);
254 }
255 
256 #endif
257 
258 #ifndef NO_QWT_WIDGETS
259 
KnobInterface(QObject * parent)260 KnobInterface::KnobInterface(QObject *parent):
261     CustomWidgetInterface(parent)
262 {
263     d_name = "QwtKnob";
264     d_include = "qwt_knob.h";
265     d_icon = QPixmap(":/pixmaps/qwtknob.png");
266     d_domXml =
267         "<widget class=\"QwtKnob\" name=\"Knob\">\n"
268         " <property name=\"geometry\">\n"
269         "  <rect>\n"
270         "   <x>0</x>\n"
271         "   <y>0</y>\n"
272         "   <width>100</width>\n"
273         "   <height>100</height>\n"
274         "  </rect>\n"
275         " </property>\n"
276         "</widget>\n";
277 }
278 
createWidget(QWidget * parent)279 QWidget *KnobInterface::createWidget(QWidget *parent)
280 {
281     return new QwtKnob(parent);
282 }
283 
284 #endif
285 
286 #ifndef NO_QWT_PLOT
287 
ScaleWidgetInterface(QObject * parent)288 ScaleWidgetInterface::ScaleWidgetInterface(QObject *parent):
289     CustomWidgetInterface(parent)
290 {
291     d_name = "QwtScaleWidget";
292     d_include = "qwt_scale_widget.h";
293     d_icon = QPixmap(":/pixmaps/qwtscale.png");
294     d_domXml =
295         "<widget class=\"QwtScaleWidget\" name=\"ScaleWidget\">\n"
296         "</widget>\n";
297 }
298 
createWidget(QWidget * parent)299 QWidget *ScaleWidgetInterface::createWidget(QWidget *parent)
300 {
301     return new QwtScaleWidget(QwtScaleDraw::LeftScale, parent);
302 }
303 
304 #endif
305 
306 #ifndef NO_QWT_WIDGETS
307 
SliderInterface(QObject * parent)308 SliderInterface::SliderInterface(QObject *parent):
309     CustomWidgetInterface(parent)
310 {
311     d_name = "QwtSlider";
312     d_include = "qwt_slider.h";
313     d_icon = QPixmap(":/pixmaps/qwtslider.png");
314     d_domXml =
315         "<widget class=\"QwtSlider\" name=\"Slider\">\n"
316         " <property name=\"geometry\">\n"
317         "  <rect>\n"
318         "   <x>0</x>\n"
319         "   <y>0</y>\n"
320         "   <width>200</width>\n"
321         "   <height>60</height>\n"
322         "  </rect>\n"
323         " </property>\n"
324         "</widget>\n";
325 }
326 
createWidget(QWidget * parent)327 QWidget *SliderInterface::createWidget(QWidget *parent)
328 {
329     QwtSlider *slider = new QwtSlider(parent);
330 #if 0
331     slider->setScalePosition(QwtSlider::Bottom);
332     slider->setRange(0.0, 10.0, 1.0, 0);
333     slider->setValue(3.0);
334 #endif
335     return slider;
336 }
337 
338 #endif
339 
TextLabelInterface(QObject * parent)340 TextLabelInterface::TextLabelInterface(QObject *parent):
341     CustomWidgetInterface(parent)
342 {
343     d_name = "QwtTextLabel";
344     d_include = "qwt_text_label.h";
345 
346     d_icon = QPixmap(":/pixmaps/qwtwidget.png");
347     d_domXml =
348         "<widget class=\"QwtTextLabel\" name=\"TextLabel\">\n"
349         " <property name=\"geometry\">\n"
350         "  <rect>\n"
351         "   <x>0</x>\n"
352         "   <y>0</y>\n"
353         "   <width>100</width>\n"
354         "   <height>20</height>\n"
355         "  </rect>\n"
356         " </property>\n"
357         "</widget>\n";
358 }
359 
createWidget(QWidget * parent)360 QWidget *TextLabelInterface::createWidget(QWidget *parent)
361 {
362     return new QwtTextLabel(parent);
363 }
364 
365 #ifndef NO_QWT_WIDGETS
366 
ThermoInterface(QObject * parent)367 ThermoInterface::ThermoInterface(QObject *parent):
368     CustomWidgetInterface(parent)
369 {
370     d_name = "QwtThermo";
371     d_include = "qwt_thermo.h";
372     d_icon = QPixmap(":/pixmaps/qwtthermo.png");
373     d_domXml =
374         "<widget class=\"QwtThermo\" name=\"Thermo\">\n"
375         "</widget>\n";
376 }
377 
createWidget(QWidget * parent)378 QWidget *ThermoInterface::createWidget(QWidget *parent)
379 {
380     return new QwtThermo(parent);
381 }
382 
383 #endif
384 
385 #ifndef NO_QWT_WIDGETS
386 
WheelInterface(QObject * parent)387 WheelInterface::WheelInterface(QObject *parent):
388     CustomWidgetInterface(parent)
389 {
390     d_name = "QwtWheel";
391     d_include = "qwt_wheel.h";
392     d_icon = QPixmap(":/pixmaps/qwtwheel.png");
393     d_domXml =
394         "<widget class=\"QwtWheel\" name=\"Wheel\">\n"
395         "</widget>\n";
396 }
397 
createWidget(QWidget * parent)398 QWidget *WheelInterface::createWidget(QWidget *parent)
399 {
400     return new QwtWheel(parent);
401 }
402 
403 #endif
404 
CustomWidgetCollectionInterface(QObject * parent)405 CustomWidgetCollectionInterface::CustomWidgetCollectionInterface(
406         QObject *parent):
407     QObject(parent)
408 {
409 #ifndef NO_QWT_PLOT
410     d_plugins.append(new PlotInterface(this));
411     d_plugins.append(new ScaleWidgetInterface(this));
412 #endif
413 
414 #ifndef NO_QWT_WIDGETS
415     d_plugins.append(new AnalogClockInterface(this));
416     d_plugins.append(new CompassInterface(this));
417     d_plugins.append(new CounterInterface(this));
418     d_plugins.append(new DialInterface(this));
419     d_plugins.append(new KnobInterface(this));
420     d_plugins.append(new SliderInterface(this));
421     d_plugins.append(new ThermoInterface(this));
422     d_plugins.append(new WheelInterface(this));
423 #endif
424 
425     d_plugins.append(new TextLabelInterface(this));
426 }
427 
428 QList<QDesignerCustomWidgetInterface*>
customWidgets(void) const429     CustomWidgetCollectionInterface::customWidgets(void) const
430 {
431     return d_plugins;
432 }
433 
TaskMenuFactory(QExtensionManager * parent)434 TaskMenuFactory::TaskMenuFactory(QExtensionManager *parent):
435     QExtensionFactory(parent)
436 {
437 }
438 
createExtension(QObject * object,const QString & iid,QObject * parent) const439 QObject *TaskMenuFactory::createExtension(
440     QObject *object, const QString &iid, QObject *parent) const
441 {
442     if (iid == Q_TYPEID(QDesignerTaskMenuExtension))
443     {
444 #ifndef NO_QWT_PLOT
445         if (QwtPlot *plot = qobject_cast<QwtPlot*>(object))
446             return new TaskMenuExtension(plot, parent);
447 #endif
448 #ifndef NO_QWT_WIDGETS
449         if (QwtDial *dial = qobject_cast<QwtDial*>(object))
450             return new TaskMenuExtension(dial, parent);
451 #endif
452     }
453 
454     return QExtensionFactory::createExtension(object, iid, parent);
455 }
456 
457 
TaskMenuExtension(QWidget * widget,QObject * parent)458 TaskMenuExtension::TaskMenuExtension(QWidget *widget, QObject *parent):
459     QObject(parent),
460     d_widget(widget)
461 {
462     d_editAction = new QAction(tr("Edit Qwt Attributes ..."), this);
463     connect(d_editAction, SIGNAL(triggered()),
464         this, SLOT(editProperties()));
465 }
466 
taskActions() const467 QList<QAction *> TaskMenuExtension::taskActions() const
468 {
469     QList<QAction *> list;
470     list.append(d_editAction);
471     return list;
472 }
473 
preferredEditAction() const474 QAction *TaskMenuExtension::preferredEditAction() const
475 {
476     return d_editAction;
477 }
478 
editProperties()479 void TaskMenuExtension::editProperties()
480 {
481     const QVariant v = d_widget->property("propertiesDocument");
482     if ( v.type() != QVariant::String )
483         return;
484 
485 #ifndef NO_QWT_PLOT
486     QString properties = v.toString();
487 
488     if ( qobject_cast<QwtPlot*>(d_widget) )
489     {
490         PlotDialog dialog(properties);
491         connect(&dialog, SIGNAL(edited(const QString&)),
492             SLOT(applyProperties(const QString &)));
493         (void)dialog.exec();
494         return;
495     }
496 #endif
497 
498     static QErrorMessage *errorMessage = NULL;
499     if ( errorMessage == NULL )
500         errorMessage = new QErrorMessage();
501     errorMessage->showMessage("Not implemented yet.");
502 }
503 
applyProperties(const QString & properties)504 void TaskMenuExtension::applyProperties(const QString &properties)
505 {
506     QDesignerFormWindowInterface *formWindow
507         = QDesignerFormWindowInterface::findFormWindow(d_widget);
508     if ( formWindow && formWindow->cursor() )
509         formWindow->cursor()->setProperty("propertiesDocument", properties);
510 }
511 
512 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
513 Q_EXPORT_PLUGIN2(QwtDesignerPlugin, CustomWidgetCollectionInterface)
514 #endif
515