1
/***************************************************************************
2
* Copyright (C) 2005-2007 Nicolas Hadacek <hadacek@kde.org> *
3
* Copyright (C) 2003-2004 Alain Gibaud <alain.gibaud@free.fr> *
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
***************************************************************************/
12
#include <qapplication.h>
13
#include <qpushbutton.h>
15
#include <qwidgetstack.h>
16
#include <qobjectlist.h>
19
#include <qmetaobject.h>
21
#include <qpopupmenu.h>
24
#include <kiconloader.h>
25
#include <kmessagebox.h>
30
#include "common/common/number.h"
31
#include "common/common/misc.h"
32
#include "common/gui/number_gui.h"
34
//-----------------------------------------------------------------------------
35
bool BusyCursor::_overridePaused = false;
37
void BusyCursor::start()
39
QApplication::setOverrideCursor(KCursor::waitCursor(), true);
42
void BusyCursor::stop()
44
QApplication::restoreOverrideCursor();
47
void BusyCursor::pause()
49
_overridePaused = QApplication::overrideCursor();
53
void BusyCursor::restore()
55
if (_overridePaused) start();
58
//-----------------------------------------------------------------------------
59
void MessageBox::information(const QString &text, Log::ShowMode show)
61
if ( show==Log::DontShow ) return;
63
KMessageBox::information(qApp->mainWidget(), text, QString::null, QString::null, KMessageBox::Notify | KMessageBox::AllowLink);
64
BusyCursor::restore();
67
void MessageBox::detailedSorry(const QString &text, const QString &details, Log::ShowMode show)
69
if ( show==Log::DontShow ) return;
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();
76
bool MessageBox::askContinue(const QString &text, const KGuiItem &buttonContinue, const QString &caption)
78
::BusyCursor::pause();
79
int res = KMessageBox::warningContinueCancel(qApp->mainWidget(), text, caption, buttonContinue);
80
::BusyCursor::restore();
81
return ( res==KMessageBox::Continue );
84
bool MessageBox::questionYesNo(const QString &text, const KGuiItem &yesButton,const KGuiItem &noButton, const QString &caption)
86
::BusyCursor::pause();
87
int res = KMessageBox::questionYesNo(qApp->mainWidget(), text, caption, yesButton, noButton);
88
::BusyCursor::restore();
89
return ( res==KMessageBox::Yes );
92
MessageBox::Result MessageBox::questionYesNoCancel(const QString &text, const KGuiItem &yesButton, const KGuiItem &noButton,
93
const QString &caption)
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;
103
void MessageBox::text(const QString &text, Log::ShowMode show)
105
if ( show==Log::DontShow ) return;
107
TextEditorDialog dialog(text, QString::null, qApp->mainWidget());
109
BusyCursor::restore();
112
//----------------------------------------------------------------------------
113
PopupButton::PopupButton(QWidget *parent, const char *name)
114
: KPushButton(parent, name)
119
PopupButton::PopupButton(const QString &text, QWidget *parent, const char *name)
120
: KPushButton(text, parent, name)
125
void PopupButton::init()
129
QPopupMenu *popup = new QPopupMenu(this);
130
connect(popup, SIGNAL(activated(int)), SIGNAL(activated(int)));
134
void PopupButton::appendAction(KAction *action)
136
if ( _separator && popup()->count()!=0 ) popup()->insertSeparator();
138
action->plug(popup());
141
void PopupButton::appendAction(const QString &label, const QString &icon,
142
QObject *receiver, const char *slot)
144
appendAction(new KAction(label, icon, 0, receiver, slot, (KActionCollection *)0));
147
void PopupButton::appendItem(const QString &label, const QString &icon, uint id)
150
QPixmap pixmap = loader.loadIcon(icon, KIcon::Small);
151
appendItem(label, pixmap, id);
154
void PopupButton::appendItem(const QString &label, const QPixmap &icon, uint id)
156
if ( _separator && popup()->count()!=0 ) popup()->insertSeparator();
158
popup()->insertItem(icon, label, id);
161
//-----------------------------------------------------------------------------
162
Splitter::Splitter(const QValueList<int> &defaultSizes, Orientation o, QWidget *parent, const char *name)
163
: QSplitter(o, parent, name), _defaultSizes(defaultSizes)
166
setOpaqueResize(true);
167
QTimer::singleShot(0, this, SLOT(updateSizes()));
170
Splitter::~Splitter()
173
gc.writeEntry(QString(name()) + "_sizes", sizes());
176
void Splitter::updateSizes()
179
QValueList<int> sizes = gc.readIntListEntry(QString(name()) + "_sizes");
180
for (uint i=sizes.count(); i<_defaultSizes.count(); i++) sizes.append(_defaultSizes[i]);
184
//-----------------------------------------------------------------------------
185
TabBar::TabBar(QWidget *parent, const char *name)
186
: KTabBar(parent, name), _ignoreWheelEvent(false)
189
void TabBar::wheelEvent(QWheelEvent *e)
191
if (_ignoreWheelEvent) QApplication::sendEvent(parent(), e); // #### not sure why ignoring is not enough...
192
else KTabBar::wheelEvent(e);
195
TabWidget::TabWidget(QWidget *parent, const char *name)
196
: KTabWidget(parent, name)
198
setTabBar(new TabBar(this));
201
void TabWidget::setIgnoreWheelEvent(bool ignore)
203
static_cast<TabBar *>(tabBar())->_ignoreWheelEvent = ignore;
206
void TabWidget::wheelEvent(QWheelEvent *e)
208
if (static_cast<TabBar *>(tabBar())->_ignoreWheelEvent) e->ignore();
209
else KTabWidget::wheelEvent(e);
212
void TabWidget::setTabBar(TabBar *tabbar)
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 )));
226
//-----------------------------------------------------------------------------
227
HexValueValidator::HexValueValidator(uint nbChars, QObject *parent)
228
: QValidator(parent, "hex_value_validator"), _nbChars(nbChars) {}
230
QValidator::State HexValueValidator::validate(QString &input, int &) const
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;
239
//-----------------------------------------------------------------------------
240
GenericHexWordEditor::GenericHexWordEditor(uint nbChars, bool hasBlankValue, QWidget *parent)
241
: KLineEdit(parent, "hex_word_editor"), _nbChars(nbChars), _hasBlankValue(hasBlankValue)
243
setFocusPolicy(ClickFocus);
244
setValidator(new HexValueValidator(nbChars, this));
245
connect(this, SIGNAL(textChanged(const QString &)), SLOT(slotTextChanged()));
249
void GenericHexWordEditor::slotTextChanged()
251
if ( text().length()!=_nbChars ) return;
252
if ( changeValue() ) emit moveNext();
255
bool GenericHexWordEditor::changeValue()
257
if ( !isValid() ) return false;
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));
267
if ( v==word() ) return false;
273
void GenericHexWordEditor::set()
276
setEnabled(isValid());
277
if ( !isValid() ) clear();
280
if ( _hasBlankValue && value==blankValue() ) setText(repeat("-", _nbChars));
281
else setText(toHex(normalizeWord(value), _nbChars));
286
bool GenericHexWordEditor::event(QEvent *e)
289
case QEvent::FocusOut:
292
case QEvent::FocusIn:
293
QTimer::singleShot(0, this, SLOT(selectAll())); // ugly but it works
295
case QEvent::KeyPress:
296
switch ( static_cast<QKeyEvent *>(e)->key() ) {
324
if ( cursorPosition()!=int(text().length()) ) break;
328
if ( cursorPosition()!=0 ) break;
334
return QLineEdit::event(e);
337
QSize GenericHexWordEditor::sizeHint() const
339
return QSize(maxCharWidth(Number::Hex, font()) * (_nbChars+1), fontMetrics().height());
342
QSize GenericHexWordEditor::minimumSizeHint() const
344
return QSize(maxCharWidth(Number::Hex, font()) * (_nbChars+1), fontMetrics().height());