summaryrefslogtreecommitdiffstats
path: root/kmag/kmagselrect.cpp
blob: 68e09b01ca4872ff18bc81af5ef5529843b58f4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/***************************************************************************
                          kmagselrect.cpp  -  description
                             -------------------
    begin                : Mon Feb 12 23:45:41 EST 2001
    copyright            : (C) 2001-2003 by Sarang Lakare
    email                : sarang#users.sf.net
    copyright            : (C) 2003-2004 by Olaf Schmidt
    email                : ojschmidt@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kmagselrect.h"
#include "kmagselrect.moc"

#include <tqapplication.h>
#include <tqcursor.h>
#include <tqdesktopwidget.h>
#include <tqpixmap.h>
#include <tqbitmap.h>

#include <kapplication.h>
#include <klocale.h>

static uchar line_bits[] = {0x2d, 0x96, 0x4b, 0xa5, 0xd2, 0x69, 0xb4, 0x5a};

static TQColor titleColor = TQColor (0,0,128);
static TQColor titleBtnColor = TQColor (255,255,0);
static TQColor textColor = TQColor (255,255,255);

static int frameSize = 10;
static int titleSize = 24;

void setTitleColors (TQColor title, TQColor text, TQColor titleBtn)
{
  titleColor = title;
  titleBtnColor = titleBtn;
  textColor = text;
}

void setFrameSize (int size)
{
  frameSize = size;
}

void setTitleSize (int size)
{
  titleSize = size;
}

TQColor getTitleColor ()
{
  return titleColor;
}

TQColor getTitleBtnColor ()
{
  return titleBtnColor;
}

TQColor getTextColor ()
{
  return textColor;
}

int getFrameSize ()
{
  return frameSize;
}

int getTitleSize ()
{
  if (titleSize > frameSize)
    return titleSize;
  else
    return frameSize;
}

//--------------------------------------------------------------------------
//   Construction
//--------------------------------------------------------------------------

KMagSelRect::KMagSelRect(TQWidget *parent) :
  TQRect()
{
  init(parent);
}

KMagSelRect::KMagSelRect(const TQPoint &topLeft, const TQPoint &bottomRight,
                 TQWidget *parent) :
TQRect(topLeft, bottomRight)
{
  init(parent);
}

KMagSelRect::KMagSelRect(const TQPoint &topLeft, const TQSize &size,
                 TQWidget *parent) :
TQRect(topLeft, size)
{
  init(parent);
}

KMagSelRect::KMagSelRect(int left, int top, int width, int height,
                 TQWidget *parent) :
TQRect(left, top, width, height)
{
  init(parent);
}

void KMagSelRect::init(TQWidget *parent)
{
  // Make sure parent is the window itself, not a widget within the window
  if (parent != 0)
    while (parent->parentWidget (true) != 0)
      parent=parent->parentWidget (true);

  selectionwindow = 0;
  selWindowParent = parent;

  m_alwaysVisible = false;
}

KMagSelRect::~KMagSelRect()
{
}

//--------------------------------------------------------------------------
//   
//--------------------------------------------------------------------------

WId KMagSelRect::winId()
{
  if (selectionwindow)
    return selectionwindow->winId();
  else
    return 0;
}

bool KMagSelRect::visible()
{
  return (selectionwindow != 0);
}

void KMagSelRect::alwaysVisible(bool visible)
{
  m_alwaysVisible = visible;
}


//--------------------------------------------------------------------------
//   Slots
//--------------------------------------------------------------------------

void KMagSelRect::show()
{
  if (selectionwindow == 0) {
    selectionwindow = new KMagSelWin (selWindowParent, "selectionwindow");
    connect (selectionwindow, TQT_SIGNAL (resized ()), this, TQT_SLOT (selWinResized ()));

    update();
    selectionwindow->show();
    selWindowParent->setActiveWindow();
  }
}

void KMagSelRect::hide()
{
  if(m_alwaysVisible)
    return;
  if (selectionwindow != 0) {
    selectionwindow->hide();
    delete selectionwindow;
    selectionwindow = 0;
  }
}

void KMagSelRect::update()
{
  // make sure the selection window does not go outside of the display
  if (height() > TQApplication::desktop()->geometry().height())
    setHeight (TQApplication::desktop()->geometry().height());
  if (width() > TQApplication::desktop()->geometry().width())
    setWidth (TQApplication::desktop()->geometry().width());

  if (top() < 0)
    moveTop (0);
  if (left() < 0)
    moveLeft (0);
  if (bottom() > TQApplication::desktop()->geometry().bottom())
    moveBottom (TQApplication::desktop()->geometry().bottom());
  if (right() > TQApplication::desktop()->geometry().right())
    moveRight (TQApplication::desktop()->geometry().right());

  if (selectionwindow != 0)
    selectionwindow->setSelRect (TQRect (topLeft(), bottomRight()));
}

void KMagSelRect::selWinResized()
{
  if (selectionwindow != 0)
  {
    TQRect newRect = selectionwindow->getSelRect();
    setRect (newRect.x(), newRect.y(), newRect.width(), newRect.height());
  }
}

//--------------------------------------------------------------------------
//   KMagSelWin
//--------------------------------------------------------------------------

KMagSelWin::KMagSelWin ( TQWidget * parent, const char * name, WFlags ) :
    TQWidget (parent, name, WStyle_Customize | WStyle_NoBorder | WStyle_StaysOnTop | WType_TopLevel | WX11BypassWM)
{
  TQBitmap line (8, 8, line_bits, true);
  setPaletteBackgroundPixmap (line);
  setBackgroundOrigin (TQWidget::WindowOrigin);

  titleBar = new KMagSelWinCorner (this, "titlebar");
  titleBar->setPaletteBackgroundColor (getTitleColor ());
  titleBar->setPaletteForegroundColor (getTextColor ());
  titleBar->setText(i18n("Selection Window")+" - "+i18n("KMagnifier"));
  connect (titleBar, TQT_SIGNAL (startResizing ()), this, TQT_SLOT (startResizing ()));
  connect (titleBar, TQT_SIGNAL (resized (TQPoint)), this, TQT_SLOT (titleMoved (TQPoint)));

  topLeftCorner = new KMagSelWinCorner (this, "topleft");
  topLeftCorner->setCursor (Qt::SizeFDiagCursor);
  topLeftCorner->setPaletteBackgroundColor (getTitleBtnColor ());
  connect (topLeftCorner, TQT_SIGNAL (startResizing ()), this, TQT_SLOT (startResizing ()));
  connect (topLeftCorner, TQT_SIGNAL (resized (TQPoint)), this, TQT_SLOT (topLeftResized (TQPoint)));

  topRightCorner = new KMagSelWinCorner (this, "topright");
  topRightCorner->setCursor (Qt::SizeBDiagCursor);
  topRightCorner->setPaletteBackgroundColor (getTitleBtnColor ());
  connect (topRightCorner, TQT_SIGNAL (startResizing ()), this, TQT_SLOT (startResizing ()));
  connect (topRightCorner, TQT_SIGNAL (resized (TQPoint)), this, TQT_SLOT (topRightResized (TQPoint)));

  bottomLeftCorner = new KMagSelWinCorner (this, "bottomleft");
  bottomLeftCorner->setCursor (Qt::SizeBDiagCursor);
  bottomLeftCorner->setPaletteBackgroundColor (getTitleBtnColor ());
  connect (bottomLeftCorner, TQT_SIGNAL (startResizing ()), this, TQT_SLOT (startResizing ()));
  connect (bottomLeftCorner, TQT_SIGNAL (resized (TQPoint)), this, TQT_SLOT (bottomLeftResized (TQPoint)));

  bottomRightCorner = new KMagSelWinCorner (this, "bottomright");
  bottomRightCorner->setCursor (Qt::SizeFDiagCursor);
  bottomRightCorner->setPaletteBackgroundColor (getTitleBtnColor ());
  connect (bottomRightCorner, TQT_SIGNAL (startResizing ()), this, TQT_SLOT (startResizing ()));
  connect (bottomRightCorner, TQT_SIGNAL (resized (TQPoint)), this, TQT_SLOT (bottomRightResized (TQPoint)));
}

KMagSelWin::~KMagSelWin()
{
  delete titleBar;
  delete topLeftCorner;
  delete topRightCorner;
  delete bottomLeftCorner;
  delete bottomRightCorner;
}

void KMagSelWin::setSelRect (TQRect selRect)
{
  selRect = selRect.normalize();

  if (selRect.left() < 0)
    selRect.setLeft (0);
  if (selRect.top() < 0)
    selRect.setTop (0);
  if (selRect.right() > TQApplication::desktop()->width())
    selRect.setRight (TQApplication::desktop()->width());
  if (selRect.bottom() > TQApplication::desktop()->height())
    selRect.setBottom (TQApplication::desktop()->height());

  setGeometry (
      selRect.left() - getFrameSize(),
      selRect.top() - getTitleSize() - 2,
      selRect.width() + getFrameSize() + getFrameSize(),
      selRect.height() + getFrameSize() + getTitleSize()+2);

  int w = getFrameSize();
  if (selRect.width() < w+w)
    w = static_cast<int>(selRect.width()/2);

  int h = getFrameSize();
  if (selRect.height() < h+h)
    h = static_cast<int>(selRect.height()/2);

  setMask (TQRegion (TQRect (0, 0, width(), height ()))
           - TQRegion (TQRect (getFrameSize(), getTitleSize()+2, selRect.width(), selRect.height()))
           - TQRegion (TQRect (0, 0, getFrameSize()+w, getTitleSize()+2-getFrameSize()))
           - TQRegion (TQRect (width()-getFrameSize()-w, 0, getFrameSize()+w, getTitleSize()+2-getFrameSize()))
           - TQRegion (TQRect (0, getTitleSize()+2+h, getFrameSize()-2, selRect.height()-h-h))
           - TQRegion (TQRect (width()-getFrameSize()+2, getTitleSize()+2+h, getFrameSize()-2, selRect.height()-h-h))
           - TQRegion (TQRect (getFrameSize()+w, height()-getFrameSize()+2, selRect.width()-w-w, getFrameSize()-2)));

  titleBar->setGeometry (getFrameSize()+w, 0, selRect.width()-h-h, getTitleSize());
  topLeftCorner->setGeometry (0, getTitleSize()+2-getFrameSize(), getFrameSize()+w, getFrameSize()+h);
  topRightCorner->setGeometry (width()-getFrameSize()-w, getTitleSize()+2-getFrameSize(), getFrameSize()+w, getFrameSize()+h);
  bottomLeftCorner->setGeometry (0, height()-getFrameSize()-h, getFrameSize()+w, getFrameSize()+h);
  bottomRightCorner->setGeometry (width()-getFrameSize()-w, height()-getFrameSize()-h, getFrameSize()+w, getFrameSize()+h);
}

TQRect KMagSelWin::getSelRect ()
{
  return TQRect (
      x() + getFrameSize(),
      y() + getTitleSize()+2,
      width() - getFrameSize() - getFrameSize(),
      height() - getFrameSize() - getTitleSize()-2);
}

void KMagSelWin::startResizing ()
{
  oldSelRect = getSelRect();
}

void KMagSelWin::titleMoved ( TQPoint offset )
{
  TQRect selRect = oldSelRect;
  selRect.moveBy (offset.x(), offset.y());
  setSelRect (selRect);
  emit resized ();
}

void KMagSelWin::topLeftResized ( TQPoint offset )
{
  setSelRect (TQRect(oldSelRect.topLeft() + offset, oldSelRect.bottomRight ()));
  emit resized();
}

void KMagSelWin::topRightResized ( TQPoint offset )
{
  setSelRect (TQRect(oldSelRect.topRight() + offset, oldSelRect.bottomLeft ()));
  emit resized();
}

void KMagSelWin::bottomLeftResized ( TQPoint offset )
{
  setSelRect (TQRect(oldSelRect.bottomLeft() + offset, oldSelRect.topRight ()));
  emit resized();
}

void KMagSelWin::bottomRightResized ( TQPoint offset )
{
  setSelRect (TQRect(oldSelRect.bottomRight() + offset, oldSelRect.topLeft()));
  emit resized();
}


//--------------------------------------------------------------------------
//   KMagSelWinCorner
//--------------------------------------------------------------------------

KMagSelWinCorner::KMagSelWinCorner ( TQWidget * parent, const char * name, WFlags f ) :
    TQLabel (parent, name, f)
{
  setFrameStyle (TQFrame::WinPanel | TQFrame::Raised);
  setLineWidth (1);
}

KMagSelWinCorner::~KMagSelWinCorner()
{
}

void KMagSelWinCorner::mousePressEvent ( TQMouseEvent * e )
{
  oldPos = e->globalPos ();
  emit startResizing ();
}

void KMagSelWinCorner::mouseReleaseEvent ( TQMouseEvent * e )
{
  setFrameShadow (TQFrame::Raised);
  emit resized (e->globalPos () - oldPos);
}

void KMagSelWinCorner::mouseMoveEvent ( TQMouseEvent * e )
{
  emit resized (e->globalPos () - oldPos);
}