~ubuntu-branches/ubuntu/karmic/piklab/karmic

« back to all changes in this revision

Viewing changes to src/common/gui/misc_gui.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Miriam Ruiz
  • Date: 2007-09-01 22:37:44 UTC
  • Revision ID: james.westby@ubuntu.com-20070901223744-2r8t5kiqdurs5j8g
Tags: upstream-0.14.5
ImportĀ upstreamĀ versionĀ 0.14.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2005-2007 Nicolas Hadacek <hadacek@kde.org>             *
 
3
 *   Copyright (C) 2003-2004 Alain Gibaud <alain.gibaud@free.fr>           *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 ***************************************************************************/
 
10
#include "misc_gui.h"
 
11
 
 
12
#include <qapplication.h>
 
13
#include <qpushbutton.h>
 
14
#include <qtimer.h>
 
15
#include <qwidgetstack.h>
 
16
#include <qobjectlist.h>
 
17
#include <qpainter.h>
 
18
#include <qheader.h>
 
19
#include <qmetaobject.h>
 
20
#include <qvariant.h>
 
21
#include <qpopupmenu.h>
 
22
 
 
23
#include <kcursor.h>
 
24
#include <kiconloader.h>
 
25
#include <kmessagebox.h>
 
26
#include <kaction.h>
 
27
#include <ktabbar.h>
 
28
 
 
29
#include "dialog.h"
 
30
#include "common/common/number.h"
 
31
#include "common/common/misc.h"
 
32
#include "common/gui/number_gui.h"
 
33
 
 
34
//-----------------------------------------------------------------------------
 
35
bool BusyCursor::_overridePaused = false;
 
36
 
 
37
void BusyCursor::start()
 
38
{
 
39
  QApplication::setOverrideCursor(KCursor::waitCursor(), true);
 
40
}
 
41
 
 
42
void BusyCursor::stop()
 
43
{
 
44
  QApplication::restoreOverrideCursor();
 
45
}
 
46
 
 
47
void BusyCursor::pause()
 
48
{
 
49
  _overridePaused = QApplication::overrideCursor();
 
50
  stop();
 
51
}
 
52
 
 
53
void BusyCursor::restore()
 
54
{
 
55
  if (_overridePaused) start();
 
56
}
 
57
 
 
58
//-----------------------------------------------------------------------------
 
59
void MessageBox::information(const QString &text, Log::ShowMode show)
 
60
{
 
61
  if ( show==Log::DontShow ) return;
 
62
  BusyCursor::pause();
 
63
  KMessageBox::information(qApp->mainWidget(), text, QString::null, QString::null, KMessageBox::Notify | KMessageBox::AllowLink);
 
64
  BusyCursor::restore();
 
65
}
 
66
 
 
67
void MessageBox::detailedSorry(const QString &text, const QString &details, Log::ShowMode show)
 
68
{
 
69
  if ( show==Log::DontShow ) return;
 
70
  BusyCursor::pause();
 
71
  if ( details.isEmpty() ) KMessageBox::sorry(qApp->mainWidget(), text, QString::null, KMessageBox::Notify | KMessageBox::AllowLink);
 
72
  else KMessageBox::detailedSorry(qApp->mainWidget(), text, details, QString::null, KMessageBox::Notify | KMessageBox::AllowLink);
 
73
  BusyCursor::restore();
 
74
}
 
75
 
 
76
bool MessageBox::askContinue(const QString &text, const KGuiItem &buttonContinue, const QString &caption)
 
77
{
 
78
  ::BusyCursor::pause();
 
79
  int res = KMessageBox::warningContinueCancel(qApp->mainWidget(), text, caption, buttonContinue);
 
80
  ::BusyCursor::restore();
 
81
  return ( res==KMessageBox::Continue );
 
82
}
 
83
 
 
84
bool MessageBox::questionYesNo(const QString &text, const KGuiItem &yesButton,const KGuiItem &noButton, const QString &caption)
 
85
{
 
86
  ::BusyCursor::pause();
 
87
  int res = KMessageBox::questionYesNo(qApp->mainWidget(), text, caption, yesButton, noButton);
 
88
  ::BusyCursor::restore();
 
89
  return ( res==KMessageBox::Yes );
 
90
}
 
91
 
 
92
MessageBox::Result MessageBox::questionYesNoCancel(const QString &text, const KGuiItem &yesButton, const KGuiItem &noButton,
 
93
                                                        const QString &caption)
 
94
{
 
95
  ::BusyCursor::pause();
 
96
  int res = KMessageBox::questionYesNoCancel(qApp->mainWidget(), text, caption, yesButton, noButton);
 
97
  ::BusyCursor::restore();
 
98
  if ( res==KMessageBox::Yes ) return Yes;
 
99
  if ( res==KMessageBox::No ) return No;
 
100
  return Cancel;
 
101
}
 
102
 
 
103
void MessageBox::text(const QString &text, Log::ShowMode show)
 
104
{
 
105
  if ( show==Log::DontShow ) return;
 
106
  BusyCursor::pause();
 
107
  TextEditorDialog dialog(text, QString::null, qApp->mainWidget());
 
108
  dialog.exec();
 
109
  BusyCursor::restore();
 
110
}
 
111
 
 
112
//----------------------------------------------------------------------------
 
113
PopupButton::PopupButton(QWidget *parent, const char *name)
 
114
  : KPushButton(parent, name)
 
115
{
 
116
  init();
 
117
}
 
118
 
 
119
PopupButton::PopupButton(const QString &text, QWidget *parent, const char *name)
 
120
  : KPushButton(text, parent, name)
 
121
{
 
122
  init();
 
123
}
 
124
 
 
125
void PopupButton::init()
 
126
{
 
127
  _separator = false;
 
128
  setFlat(true);
 
129
  QPopupMenu *popup = new QPopupMenu(this);
 
130
  connect(popup, SIGNAL(activated(int)), SIGNAL(activated(int)));
 
131
  setPopup(popup);
 
132
}
 
133
 
 
134
void PopupButton::appendAction(KAction *action)
 
135
{
 
136
  if ( _separator && popup()->count()!=0 ) popup()->insertSeparator();
 
137
  _separator = false;
 
138
  action->plug(popup());
 
139
}
 
140
 
 
141
void PopupButton::appendAction(const QString &label, const QString &icon,
 
142
                               QObject *receiver, const char *slot)
 
143
{
 
144
  appendAction(new KAction(label, icon, 0, receiver, slot, (KActionCollection *)0));
 
145
}
 
146
 
 
147
void PopupButton::appendItem(const QString &label, const QString &icon, uint id)
 
148
{
 
149
  KIconLoader loader;
 
150
  QPixmap pixmap = loader.loadIcon(icon, KIcon::Small);
 
151
  appendItem(label, pixmap, id);
 
152
}
 
153
 
 
154
void PopupButton::appendItem(const QString &label, const QPixmap &icon, uint id)
 
155
{
 
156
  if ( _separator && popup()->count()!=0 ) popup()->insertSeparator();
 
157
  _separator = false;
 
158
  popup()->insertItem(icon, label, id);
 
159
}
 
160
 
 
161
//-----------------------------------------------------------------------------
 
162
Splitter::Splitter(const QValueList<int> &defaultSizes, Orientation o, QWidget *parent, const char *name)
 
163
  : QSplitter(o, parent, name), _defaultSizes(defaultSizes)
 
164
{
 
165
  Q_ASSERT(name);
 
166
  setOpaqueResize(true);
 
167
  QTimer::singleShot(0, this, SLOT(updateSizes()));
 
168
}
 
169
 
 
170
Splitter::~Splitter()
 
171
{
 
172
  GuiConfig gc;
 
173
  gc.writeEntry(QString(name()) + "_sizes", sizes());
 
174
}
 
175
 
 
176
void Splitter::updateSizes()
 
177
{
 
178
  GuiConfig gc;
 
179
  QValueList<int> sizes = gc.readIntListEntry(QString(name()) + "_sizes");
 
180
  for (uint i=sizes.count(); i<_defaultSizes.count(); i++) sizes.append(_defaultSizes[i]);
 
181
  setSizes(sizes);
 
182
}
 
183
 
 
184
//-----------------------------------------------------------------------------
 
185
TabBar::TabBar(QWidget *parent, const char *name)
 
186
  : KTabBar(parent, name), _ignoreWheelEvent(false)
 
187
{}
 
188
 
 
189
void TabBar::wheelEvent(QWheelEvent *e)
 
190
{
 
191
  if (_ignoreWheelEvent) QApplication::sendEvent(parent(), e); // #### not sure why ignoring is not enough...
 
192
  else KTabBar::wheelEvent(e);
 
193
}
 
194
 
 
195
TabWidget::TabWidget(QWidget *parent, const char *name)
 
196
  : KTabWidget(parent, name)
 
197
{
 
198
  setTabBar(new TabBar(this));
 
199
}
 
200
 
 
201
void TabWidget::setIgnoreWheelEvent(bool ignore)
 
202
{
 
203
  static_cast<TabBar *>(tabBar())->_ignoreWheelEvent = ignore;
 
204
}
 
205
 
 
206
void TabWidget::wheelEvent(QWheelEvent *e)
 
207
{
 
208
  if (static_cast<TabBar *>(tabBar())->_ignoreWheelEvent) e->ignore();
 
209
  else KTabWidget::wheelEvent(e);
 
210
}
 
211
 
 
212
void TabWidget::setTabBar(TabBar *tabbar)
 
213
{
 
214
  KTabWidget::setTabBar(tabbar);
 
215
  connect(tabBar(), SIGNAL(contextMenu( int, const QPoint & )), SLOT(contextMenu( int, const QPoint & )));
 
216
  connect(tabBar(), SIGNAL(mouseDoubleClick( int )), SLOT(mouseDoubleClick( int )));
 
217
  connect(tabBar(), SIGNAL(mouseMiddleClick( int )), SLOT(mouseMiddleClick( int )));
 
218
  connect(tabBar(), SIGNAL(initiateDrag( int )), SLOT(initiateDrag( int )));
 
219
  connect(tabBar(), SIGNAL(testCanDecode(const QDragMoveEvent *, bool & )), SIGNAL(testCanDecode(const QDragMoveEvent *, bool & )));
 
220
  connect(tabBar(), SIGNAL(receivedDropEvent( int, QDropEvent * )), SLOT(receivedDropEvent( int, QDropEvent * )));
 
221
  connect(tabBar(), SIGNAL(moveTab( int, int )), SLOT(moveTab( int, int )));
 
222
  connect(tabBar(), SIGNAL(closeRequest( int )), SLOT(closeRequest( int )));
 
223
  connect(tabBar(), SIGNAL(wheelDelta( int )), SLOT(wheelDelta( int )));
 
224
}
 
225
 
 
226
//-----------------------------------------------------------------------------
 
227
HexValueValidator::HexValueValidator(uint nbChars, QObject *parent)
 
228
  : QValidator(parent, "hex_value_validator"), _nbChars(nbChars) {}
 
229
 
 
230
QValidator::State HexValueValidator::validate(QString &input, int &) const
 
231
{
 
232
  if ( input.length()==0 ) return Acceptable;
 
233
  if ( input.length()>_nbChars ) return Invalid;
 
234
  for (uint i=0; i<input.length(); i++)
 
235
    if ( !isxdigit(input[i].latin1()) && input[i]!='-' ) return Invalid;
 
236
  return Acceptable;
 
237
}
 
238
 
 
239
//-----------------------------------------------------------------------------
 
240
GenericHexWordEditor::GenericHexWordEditor(uint nbChars, bool hasBlankValue, QWidget *parent)
 
241
  : KLineEdit(parent, "hex_word_editor"), _nbChars(nbChars), _hasBlankValue(hasBlankValue)
 
242
{
 
243
  setFocusPolicy(ClickFocus);
 
244
  setValidator(new HexValueValidator(nbChars, this));
 
245
  connect(this, SIGNAL(textChanged(const QString &)), SLOT(slotTextChanged()));
 
246
  setFrame(false);
 
247
}
 
248
 
 
249
void GenericHexWordEditor::slotTextChanged()
 
250
{
 
251
  if ( text().length()!=_nbChars ) return;
 
252
  if ( changeValue() ) emit moveNext();
 
253
}
 
254
 
 
255
bool GenericHexWordEditor::changeValue()
 
256
{
 
257
  if ( !isValid() ) return false;
 
258
  QString s = text();
 
259
  uint v = blankValue();
 
260
  if ( s!=QString(repeat("-", _nbChars)) ) {
 
261
    s = s.leftJustify(_nbChars, '0', true);
 
262
    for (uint i=0; i<_nbChars; i++)
 
263
      if ( !isxdigit(s[i].latin1()) ) s[i] = '0';
 
264
    v = normalizeWord(fromHex(s, 0));
 
265
    setText(toHex(v, _nbChars));
 
266
  }
 
267
  if ( v==word() ) return false;
 
268
  setWord(v);
 
269
  emit modified();
 
270
  return true;
 
271
}
 
272
 
 
273
void GenericHexWordEditor::set()
 
274
{
 
275
  blockSignals(true);
 
276
  setEnabled(isValid());
 
277
  if ( !isValid() ) clear();
 
278
  else {
 
279
    uint value = word();
 
280
    if ( _hasBlankValue && value==blankValue() ) setText(repeat("-", _nbChars));
 
281
    else setText(toHex(normalizeWord(value), _nbChars));
 
282
  }
 
283
  blockSignals(false);
 
284
}
 
285
 
 
286
bool GenericHexWordEditor::event(QEvent *e)
 
287
{
 
288
  switch (e->type()) {
 
289
  case QEvent::FocusOut:
 
290
    changeValue();
 
291
    break;
 
292
  case QEvent::FocusIn:
 
293
    QTimer::singleShot(0, this, SLOT(selectAll())); // ugly but it works
 
294
    break;
 
295
  case QEvent::KeyPress:
 
296
    switch ( static_cast<QKeyEvent *>(e)->key() ) {
 
297
      case Key_Next:
 
298
        emit moveNextPage();
 
299
        return true;
 
300
      case Key_Tab:
 
301
      case Key_Enter:
 
302
      case Key_Return:
 
303
        emit moveNext();
 
304
        return true;
 
305
      case Key_Prior:
 
306
        emit movePrevPage();
 
307
        return true;
 
308
      case Key_BackTab:
 
309
        emit movePrev();
 
310
        return true;
 
311
      case Key_Down:
 
312
        emit moveDown();
 
313
        return true;
 
314
      case Key_Up:
 
315
        emit moveUp();
 
316
        return true;
 
317
      case Key_Home:
 
318
        emit moveFirst();
 
319
        return true;
 
320
      case Key_End:
 
321
        emit moveLast();
 
322
        return true;
 
323
      case Key_Right:
 
324
        if ( cursorPosition()!=int(text().length()) ) break;
 
325
        emit moveNext();
 
326
        return true;
 
327
      case Key_Left:
 
328
        if ( cursorPosition()!=0 ) break;
 
329
        emit movePrev();
 
330
        return true;
 
331
    }
 
332
   default: break;
 
333
  }
 
334
  return QLineEdit::event(e);
 
335
}
 
336
 
 
337
QSize GenericHexWordEditor::sizeHint() const
 
338
{
 
339
  return QSize(maxCharWidth(Number::Hex, font()) * (_nbChars+1), fontMetrics().height());
 
340
}
 
341
 
 
342
QSize GenericHexWordEditor::minimumSizeHint() const
 
343
{
 
344
  return QSize(maxCharWidth(Number::Hex, font()) * (_nbChars+1), fontMetrics().height());
 
345
}