Developer Documentation

toolBoxElement.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42
43
44 //== INCLUDES =================================================================
45 #include <QVBoxLayout>
46 #include <QLabel>
47 #include <QPainter>
48 #include <QMimeData>
49 #include <QDrag>
50 #include <QMouseEvent>
51
52 #include "toolBoxElement.hh"
53 #include "parser/element.hh"
54
55 #define BACKGROUND_RED 0x00
56 #define BACKGROUND_GREEN 0x00
57 #define BACKGROUND_BLUE 0x00
58 #define BACKGROUND_ALPHA 0xff
59
60 //== NAMESPACES ===============================================================
61 namespace VSI {
62
63 //=============================================================================
64 //
65 // CLASS ToolBoxElement - IMPLEMENTATION
66 //
67 //=============================================================================
68
70 ToolBoxElement::ToolBoxElement (Element *_element, QWidget *_parent) :
71 QWidget (_parent),
72 element_ (_element)
73 {
74 QVBoxLayout *layout = new QVBoxLayout ();
75
76 layout->setContentsMargins (2, 2, 2, 2);
77
78 QLabel *label = new QLabel (element_->shortDescription (), this);
79
80 label->setAlignment (Qt::AlignHCenter);
81 label->setWordWrap (true);
82
83 QPalette p = label->palette ();
84 p.setBrush (QPalette::WindowText, Qt::white);
85 p.setBrush (QPalette::ButtonText, Qt::white);
86 label->setPalette (p);
87
88 layout->addWidget (label);
89
90 setToolTip (element_->longDescription ());
91
92 setLayout (layout);
93 }
94
95 //------------------------------------------------------------------------------
96
98 ToolBoxElement::~ToolBoxElement ()
99 {
100 }
101
102 //------------------------------------------------------------------------------
103
104 // Paint a rounded rect as background
105 void ToolBoxElement::paintEvent(QPaintEvent * /*_event*/)
106 {
107 QPainter p (this);
108 p.setRenderHint(QPainter::Antialiasing);
109
110 p.setBrush(QBrush(QColor(BACKGROUND_RED,
111 BACKGROUND_GREEN,
112 BACKGROUND_BLUE,
113 BACKGROUND_ALPHA)));
114 p.setPen(QColor(BACKGROUND_RED + 0x30,
115 BACKGROUND_GREEN + 0x30,
116 BACKGROUND_BLUE + 0x30,
117 BACKGROUND_ALPHA));
118 p.drawRoundedRect(0, 0, width (), height (), 4, 4);
119
120 }
121
122 //------------------------------------------------------------------------------
123
125 void ToolBoxElement::mousePressEvent(QMouseEvent *_event)
126 {
127 QMimeData *mimeData = new QMimeData;
128
129 QPoint off = _event->pos() - QPoint (width () / 2, height () / 2);
130
131 QString data = QString::number (off.x ()) + ";";
132 data += QString::number (off.y ()) + ";" + element_->name ();
133
134 // internal mimetype
135 mimeData->setData ("application/x-openflipper.vsi",data.toLocal8Bit ());
136
137 QDrag *drag = new QDrag(this);
138 drag->setMimeData(mimeData);
139
140 QPixmap pixmap (size ());
141 pixmap.fill(Qt::transparent);
142
143 render (&pixmap);
144
145 drag->setPixmap(pixmap);
146 drag->setHotSpot(_event->pos());
147
148 drag->exec ();
149 }
150
151 //------------------------------------------------------------------------------
152 }
153
VSI::Element::longDescription
const QString & longDescription() const
Long description.
Definition: element.hh:91
VSI::ToolBoxElement::~ToolBoxElement
~ToolBoxElement()
Destructor.
Definition: toolBoxElement.cc:98
VSI::Element::shortDescription
const QString & shortDescription() const
Short description.
Definition: element.hh:88
VSI::ToolBoxElement::ToolBoxElement
ToolBoxElement(Element *_element, QWidget *_parent=NULL)
Constructor.
Definition: toolBoxElement.cc:70
VSI::Element::name
QString name() const
Element name.
Definition: element.hh:82
VSI
Definition: baseWidget.cc:72
VSI::Element
Definition: element.hh:68
VSI::ToolBoxElement::mousePressEvent
void mousePressEvent(QMouseEvent *_event)
Starts drag on mouse press.
Definition: toolBoxElement.cc:125