stencilbarbutton.cpp
1/*
2 * Kivio - Visual Modelling and Flowcharting
3 * Copyright (C) 2000-2001 theKompany.com & Dave Marotti
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19#include "stencilbarbutton.h"
20
21#include <kiconloader.h>
22#include <kstringhandler.h>
23#include <tqapplication.h>
24#include <tqstyle.h>
25#include <tqpainter.h>
26#include <tqpixmap.h>
27#include <tqtooltip.h>
28#include <kdebug.h>
29#include "tqdrawutil.h"
30
31using namespace Kivio;
32
33DragBarButton::DragBarButton( const TQString& text, TQWidget* parent, const char* name )
34: TQPushButton(text,parent,name)
35{
36 m_pIcon = 0L;
37 m_bPressed = false;
38 m_bDragged = false;
39 m_bMouseOn = false;
40 m_bClose = false;
41 m_orientation = TQt::Vertical;
42
43 const char* stencil_xpm[] = {
44 "12 12 17 1",
45 ". c #08009A",
46 "+ c #9A9C10",
47 "@ c None",
48 "# c #0800A1",
49 "$ c #FAFD00",
50 "% c #080092",
51 "& c #F9FC07",
52 "* c #F9FC00",
53 "= c #070094",
54 "- c #F4F70C",
55 "; c #F6F900",
56 "> c #08008D",
57 ", c #F7FA00",
58 "' c #C00101",
59 ") c #C20904",
60 "! c #C90000",
61 "~ c #BC0000",
62 ".......+++@@",
63 "..#...$$$$+@",
64 "..%..&$***$+",
65 ".=..-***$;*+",
66 ".>.$**$*$,*+",
67 "..;**$$$$,*+",
68 ".$')!!!!!!!~",
69 "..-!!!!!!!!+",
70 "...-!!!!!!!+",
71 "....-!!!!!++",
72 "....%-!!+++@",
73 "......++++@@"};
74
75 setPixmap(stencil_xpm);
76
77 const TQColorGroup& g = colorGroup();
78 TQColor c = g.buttonText();
79
80 int cr, cg, cb;
81 TQString line;
82 c.rgb( &cr, &cg, &cb );
83 line.sprintf(". c #%02X%02X%02X",cr,cg,cb);
84
85 const char* close_xpm[] = {
86 "8 7 2 1",
87 "x c None",
88 (const char*)line.ascii(),
89 "xxxxxxxx",
90 "x..xx..x",
91 "xx....xx",
92 "xxx..xxx",
93 "xx....xx",
94 "x..xx..x",
95 "xxxxxxxx"
96 };
97 m_pClosePix = new TQPixmap(close_xpm);
98
99 TQToolTip::add(this, text);
100}
101
102DragBarButton::~DragBarButton()
103{
104 delete m_pClosePix;
105 delete m_pIcon;
106
107 kdDebug(43000) << "DragBarButton - AHHHHHH I'M DYING!" << endl;
108}
109
110void DragBarButton::drawButton( TQPainter* paint )
111{
112 TQSize pixSize;
113
114 if(m_orientation == TQt::Vertical) {
115 pixSize = TQSize(width(), height());
116 } else {
117 pixSize = TQSize(height(), width());
118 }
119
120 TQPixmap pix(pixSize.width(), pixSize.height());
121 pix.fill(eraseColor());
122 TQPainter pixPainter(&pix);
123
124 const TQColorGroup& g = colorGroup();
125 style().drawControl( TQStyle::CE_PushButton, &pixPainter, this, TQRect(0, 0, pixSize.width(), pixSize.height()), colorGroup() );
126
127 int m = 3;
128 int tw = 0;
129 int pw = 0;
130
131 if ( m_pIcon ) {
132 pw = m_pIcon->width();
133 style().drawItem( &pixPainter, TQRect( m, 0, pw, pixSize.height() ),
134 AlignLeft | AlignVCenter,
135 colorGroup(), isEnabled(),
136 m_pIcon, TQString(), -1,
137 &g.buttonText());
138 }
139
140 if ( !text().isEmpty() ) {
141 TQFontMetrics fm = fontMetrics();
142 TQSize sz = fm.size( ShowPrefix, text() );
143 tw = sz.width();
144 int x = m + pw + (tw!=0 && pw!=0 ?m:0);
145 TQString t = KStringHandler::rPixelSqueeze(text(), fm, pixSize.width() - (x + m + 22));
146
147 style().drawItem( &pixPainter, TQRect( x, 0, tw, pixSize.height() ),
148 AlignLeft | AlignVCenter|ShowPrefix,
149 colorGroup(), isEnabled(),
150 0L, t, -1,
151 &g.buttonText());
152 }
153
154
155 TQPoint p1(pixSize.width() - 10, 0);
156 TQPoint p2(pixSize.width() - 10, pixSize.height());
157 qDrawShadeLine( &pixPainter, p1, p2, g, true, 0, 1 );
158
159 p1 += TQPoint(2, 0);
160 p2 += TQPoint(2, 0);
161 qDrawShadeLine( &pixPainter, p1, p2, g, true, 0, 1 );
162
163 if (m_bMouseOn) {
164 int z = m_bClose ? 1:0;
165 pixPainter.drawPixmap(pixSize.width() - 20 + z, (pixSize.height() - m_pClosePix->height()) / 2 + z, *m_pClosePix);
166 }
167
168 if(m_orientation == TQt::Vertical) {
169 paint->drawPixmap(0, 0, pix);
170 } else {
171 paint->rotate(-90);
172 paint->drawPixmap(1 - pixSize.width(), 0, pix);
173 }
174}
175
176void DragBarButton::setIcon( const TQString& name )
177{
178 m_pIcon = new TQPixmap(BarIcon(name));
179}
180
181void DragBarButton::setPixmap( const TQPixmap& pixmap )
182{
183 m_pIcon = new TQPixmap(pixmap);
184}
185
186TQSize DragBarButton::sizeHint() const
187{
188 constPolish();
189 int m = 3;
190 int tw = 0;
191 int th = 0;
192 int pw = 0;
193 int ph = 0;
194
195 if ( !text().isEmpty() ) {
196 TQFontMetrics fm = fontMetrics();
197 TQSize sz = fm.size( ShowPrefix, text() );
198 tw = 64; // Minimum size
199 th = sz.height();
200 }
201
202 if ( m_pIcon ) {
203 pw = m_pIcon->width();
204 ph = m_pIcon->height();
205 }
206
207 int h = TQMAX(ph,th)+2*m;
208 int w = m + pw + (tw!=0 && pw!=0 ?m:0) + tw + m + 22;
209
210 TQSize size;
211
212 if(m_orientation == TQt::Vertical) {
213 size = TQSize(w, h).expandedTo(TQApplication::globalStrut());
214 } else {
215 size = TQSize(h, w).expandedTo(TQApplication::globalStrut());
216 }
217
218 return size;
219}
220
221void DragBarButton::mousePressEvent( TQMouseEvent* ev )
222{
223 m_bClose = false;
224 TQRect closeRect;
225
226 if(m_orientation == TQt::Vertical) {
227 closeRect.setRect(width() - 20, 0, m_pClosePix->width(), height());
228 } else {
229 closeRect.setRect(0, 20 - m_pClosePix->width(), width(), m_pClosePix->width());
230 }
231
232 if ( closeRect.contains(ev->pos())) {
233 m_bClose = true;
234 repaint();
235 return;
236 }
237
238 m_bPressed = true;
239 m_ppoint = ev->pos();
240}
241
242void DragBarButton::mouseReleaseEvent( TQMouseEvent* ev )
243{
244 if ( m_bClose ) {
245 m_bClose = false;
246 repaint();
247 TQRect closeRect;
248
249 if(m_orientation == TQt::Vertical) {
250 closeRect.setRect(width() - 20, 0, m_pClosePix->width(), height());
251 } else {
252 closeRect.setRect(0, 20 - m_pClosePix->width(), width(), m_pClosePix->width());
253 }
254
255 if ( closeRect.contains(ev->pos()))
256 {
257 kdDebug(43000) << "DragBarButton::mouseReleaseEvent() - Emitting closeRequest" << endl;
258 emit closeRequired(this);
259 }
260 return;
261 }
262
263 m_bPressed = false;
264 if (m_bDragged) {
265 m_bDragged = false;
266 emit finishDrag();
267 } else {
268 emit clicked();
269 }
270}
271
272void DragBarButton::mouseMoveEvent( TQMouseEvent* ev )
273{
274 if (m_bPressed) {
275 TQPoint p = m_ppoint;
276 p -= ev->pos();
277 if (p.manhattanLength() > 2 && !m_bDragged ) {
278 m_bDragged = true;
279 emit beginDrag();
280 }
281 }
282}
283
284void DragBarButton::enterEvent( TQEvent* ev )
285{
286 TQPushButton::enterEvent(ev);
287 m_bMouseOn = true;
288 repaint(false);
289}
290
291void DragBarButton::leaveEvent( TQEvent* ev )
292{
293 TQPushButton::leaveEvent(ev);
294 m_bMouseOn = false;
295 repaint();
296}
297
298void DragBarButton::setOrientation(TQt::Orientation orientation)
299{
300 m_orientation = orientation;
301
302 if(m_orientation == TQt::Vertical) {
303 setSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::Fixed);
304 } else {
305 setSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Minimum);
306 }
307}
308
309#include "stencilbarbutton.moc"