1
/***************************************************************************
2
* Copyright (c) 2006 Werner Mayer <werner.wm.mayer@gmx.de> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
26
#include <QFileDialog>
27
#include <QMessageBox>
28
#include <QStyleOptionButton>
29
#include <QStylePainter>
31
#include "customwidgets.h"
36
UrlLabel::UrlLabel ( QWidget * parent, Qt::WFlags f )
37
: QLabel("TextLabel", parent, f)
39
_url = "http://localhost";
40
setToolTip(this->_url);
47
void UrlLabel::enterEvent ( QEvent * )
49
setCursor(Qt::PointingHandCursor);
52
void UrlLabel::leaveEvent ( QEvent * )
54
setCursor(Qt::ArrowCursor);
57
void UrlLabel::mouseReleaseEvent ( QMouseEvent * )
59
QMessageBox::information(this, "Browser",
60
QString("This starts your browser with url %1").arg(_url));
63
QString UrlLabel::url() const
68
void UrlLabel::setUrl(const QString& u)
71
setToolTip(this->_url);
74
FileChooser::FileChooser( QWidget *parent )
75
: QWidget( parent ), md( File ), _filter( QString::null )
77
QHBoxLayout *layout = new QHBoxLayout( this );
78
layout->setMargin( 0 );
79
layout->setSpacing( 6 );
81
lineEdit = new QLineEdit( this );
82
layout->addWidget( lineEdit );
84
connect(lineEdit, SIGNAL(textChanged(const QString &)),
85
this, SIGNAL(fileNameChanged(const QString &)));
87
button = new QPushButton( "...", this );
88
button->setFixedWidth(2*button->fontMetrics().width( " ... " ));
89
layout->addWidget( button );
91
connect(button, SIGNAL(clicked()), this, SLOT(chooseFile()));
93
setFocusProxy( lineEdit );
96
FileChooser::~FileChooser()
100
QString FileChooser::fileName() const
102
return lineEdit->text();
105
void FileChooser::setFileName( const QString &fn )
107
lineEdit->setText( fn );
110
void FileChooser::chooseFile()
113
if ( mode() == File )
114
fn = QFileDialog::getOpenFileName(this, tr("Select a file"),
115
lineEdit->text(), _filter);
117
fn = QFileDialog::getExistingDirectory(this, tr("Select a directory"),
121
lineEdit->setText(fn);
122
emit fileNameSelected(fn);
126
FileChooser::Mode FileChooser::mode() const
131
void FileChooser::setMode( Mode m )
136
QString FileChooser::filter() const
141
void FileChooser::setFilter ( const QString& filter )
146
void FileChooser::setButtonText( const QString& txt )
148
button->setText( txt );
149
int w1 = 2*button->fontMetrics().width(txt);
150
int w2 = 2*button->fontMetrics().width(" ... ");
151
button->setFixedWidth((w1 > w2 ? w1 : w2));
154
QString FileChooser::buttonText() const
156
return button->text();
159
// ------------------------------------------------------------------------------
161
PrefFileChooser::PrefFileChooser ( QWidget * parent )
162
: FileChooser(parent)
166
PrefFileChooser::~PrefFileChooser()
170
QByteArray PrefFileChooser::entryName () const
175
QByteArray PrefFileChooser::paramGrpPath () const
180
void PrefFileChooser::setEntryName ( const QByteArray& name )
185
void PrefFileChooser::setParamGrpPath ( const QByteArray& name )
190
// --------------------------------------------------------------------
192
AccelLineEdit::AccelLineEdit ( QWidget * parent )
198
void AccelLineEdit::keyPressEvent ( QKeyEvent * e)
204
Qt::KeyboardModifiers state = e->modifiers();
206
if ( key == Qt::Key_Control )
208
else if ( key == Qt::Key_Shift )
210
else if ( key == Qt::Key_Alt )
212
else if ( state == Qt::NoModifier && key == Qt::Key_Backspace )
213
return; // clears the edit field
217
case Qt::ControlModifier:
219
QKeySequence key(Qt::CTRL+key);
220
txt += (QString)(key);
223
case Qt::AltModifier:
225
QKeySequence key(Qt::ALT+key);
226
txt += (QString)(key);
229
case Qt::ShiftModifier:
231
QKeySequence key(Qt::SHIFT+key);
232
txt += (QString)(key);
235
case Qt::ControlModifier+Qt::AltModifier:
237
QKeySequence key(Qt::CTRL+Qt::ALT+key);
238
txt += (QString)(key);
241
case Qt::ControlModifier+Qt::ShiftModifier:
243
QKeySequence key(Qt::CTRL+Qt::SHIFT+key);
244
txt += (QString)(key);
247
case Qt::ShiftModifier+Qt::AltModifier:
249
QKeySequence key(Qt::SHIFT+Qt::ALT+key);
250
txt += (QString)(key);
253
case Qt::ControlModifier+Qt::AltModifier+Qt::ShiftModifier:
255
QKeySequence key(Qt::CTRL+Qt::ALT+Qt::SHIFT+key);
256
txt += (QString)(key);
261
QKeySequence key(key);
262
txt += (QString)(key);
268
// ------------------------------------------------------------------------------
270
CommandIconView::CommandIconView ( QWidget * parent )
271
: QListWidget(parent)
273
connect(this, SIGNAL (currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
274
this, SLOT (onSelectionChanged(QListWidgetItem *, QListWidgetItem *)) );
277
CommandIconView::~CommandIconView ()
281
void CommandIconView::startDrag ( Qt::DropActions /*supportedActions*/ )
283
QList<QListWidgetItem*> items = selectedItems();
285
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
288
dataStream << items.count();
289
for (QList<QListWidgetItem*>::ConstIterator it = items.begin(); it != items.end(); ++it) {
290
if (it == items.begin())
291
pixmap = qVariantValue<QPixmap>((*it)->data(Qt::UserRole));
292
dataStream << (*it)->text();
295
QMimeData *mimeData = new QMimeData;
296
mimeData->setData("text/x-action-items", itemData);
298
QDrag *drag = new QDrag(this);
299
drag->setMimeData(mimeData);
300
drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()/2));
301
drag->setPixmap(pixmap);
302
drag->start(Qt::MoveAction);
305
void CommandIconView::onSelectionChanged(QListWidgetItem * item, QListWidgetItem *)
308
emitSelectionChanged(item->toolTip());
311
// ------------------------------------------------------------------------------
315
class UnsignedValidator : public QValidator
318
UnsignedValidator( QObject * parent );
319
UnsignedValidator( uint minimum, uint maximum, QObject * parent );
320
~UnsignedValidator();
322
QValidator::State validate( QString &, int & ) const;
324
void setBottom( uint );
326
virtual void setRange( uint bottom, uint top );
328
uint bottom() const { return b; }
329
uint top() const { return t; }
335
UnsignedValidator::UnsignedValidator( QObject * parent )
336
: QValidator( parent )
342
UnsignedValidator::UnsignedValidator( uint minimum, uint maximum, QObject * parent )
343
: QValidator( parent )
349
UnsignedValidator::~UnsignedValidator()
354
QValidator::State UnsignedValidator::validate( QString & input, int & ) const
356
QString stripped;// = input.stripWhiteSpace();
357
if ( stripped.isEmpty() )
360
uint entered = input.toUInt( &ok );
363
else if ( entered < b )
365
else if ( entered > t )
367
// else if ( entered < b || entered > t )
373
void UnsignedValidator::setRange( uint minimum, uint maximum )
379
void UnsignedValidator::setBottom( uint bottom )
381
setRange( bottom, top() );
384
void UnsignedValidator::setTop( uint top )
386
setRange( bottom(), top );
389
class UIntSpinBoxPrivate
392
UnsignedValidator * mValidator;
394
UIntSpinBoxPrivate() : mValidator(0)
397
uint mapToUInt( int v ) const
400
if ( v == INT_MIN ) {
403
else if ( v == INT_MAX ) {
407
v -= INT_MIN; ui = (uint)v;
410
ui = (uint)v; ui -= INT_MIN;
414
int mapToInt( uint v ) const
417
if ( v == UINT_MAX ) {
423
else if ( v > INT_MAX ) {
424
v += INT_MIN; in = (int)v;
427
in = v; in += INT_MIN;
435
// -------------------------------------------------------------
437
UIntSpinBox::UIntSpinBox (QWidget* parent)
440
d = new UIntSpinBoxPrivate;
441
d->mValidator = new UnsignedValidator(this->minimum(), this->maximum(), this);
442
connect(this, SIGNAL(valueChanged(int)),
443
this, SLOT(valueChange(int)));
449
UIntSpinBox::~UIntSpinBox()
451
delete d->mValidator;
455
void UIntSpinBox::setRange(uint minVal, uint maxVal)
457
int iminVal = d->mapToInt(minVal);
458
int imaxVal = d->mapToInt(maxVal);
459
QSpinBox::setRange(iminVal, imaxVal);
463
QValidator::State UIntSpinBox::validate (QString & input, int & pos) const
465
return d->mValidator->validate(input, pos);
468
uint UIntSpinBox::value() const
470
return d->mapToUInt(QSpinBox::value());
473
void UIntSpinBox::setValue(uint value)
475
QSpinBox::setValue(d->mapToInt(value));
478
void UIntSpinBox::valueChange(int value)
480
valueChanged(d->mapToUInt(value));
483
uint UIntSpinBox::minimum() const
485
return d->mapToUInt(QSpinBox::minimum());
488
void UIntSpinBox::setMinimum(uint minVal)
490
uint maxVal = maximum();
493
setRange(minVal, maxVal);
496
uint UIntSpinBox::maximum() const
498
return d->mapToUInt(QSpinBox::maximum());
501
void UIntSpinBox::setMaximum(uint maxVal)
503
uint minVal = minimum();
506
setRange(minVal, maxVal);
509
QString UIntSpinBox::textFromValue (int v) const
511
uint val = d->mapToUInt(v);
517
int UIntSpinBox::valueFromText (const QString & text) const
521
uint newVal = s.toUInt(&ok);
522
if (!ok && !(prefix().isEmpty() && suffix().isEmpty())) {
524
newVal = s.toUInt(&ok);
527
return d->mapToInt(newVal);
530
void UIntSpinBox::updateValidator()
532
d->mValidator->setRange(this->minimum(), this->maximum());
535
// --------------------------------------------------------------------
537
PrefSpinBox::PrefSpinBox ( QWidget * parent )
542
PrefSpinBox::~PrefSpinBox()
546
QByteArray PrefSpinBox::entryName () const
551
QByteArray PrefSpinBox::paramGrpPath () const
556
void PrefSpinBox::setEntryName ( const QByteArray& name )
561
void PrefSpinBox::setParamGrpPath ( const QByteArray& name )
566
// --------------------------------------------------------------------
568
PrefDoubleSpinBox::PrefDoubleSpinBox ( QWidget * parent )
569
: QDoubleSpinBox(parent)
573
PrefDoubleSpinBox::~PrefDoubleSpinBox()
577
QByteArray PrefDoubleSpinBox::entryName () const
582
QByteArray PrefDoubleSpinBox::paramGrpPath () const
587
void PrefDoubleSpinBox::setEntryName ( const QByteArray& name )
592
void PrefDoubleSpinBox::setParamGrpPath ( const QByteArray& name )
597
// -------------------------------------------------------------
599
ColorButton::ColorButton(QWidget* parent)
600
: QPushButton( parent ), _allowChange(true), _drawFrame(true)
602
_col = palette().color(QPalette::Active,QPalette::Midlight);
603
connect( this, SIGNAL( clicked() ), SLOT( onChooseColor() ));
606
ColorButton::~ColorButton()
610
void ColorButton::setColor( const QColor& c )
616
QColor ColorButton::color() const
621
void ColorButton::setAllowChangeColor(bool ok)
626
bool ColorButton::allowChangeColor() const
631
void ColorButton::setDrawFrame(bool ok)
636
bool ColorButton::drawFrame() const
641
void ColorButton::paintEvent ( QPaintEvent * e )
643
// first paint the complete button
644
QPushButton::paintEvent(e);
646
// repaint the rectangle area
647
QPalette::ColorGroup group = isEnabled() ? hasFocus() ? QPalette::Active : QPalette::Inactive : QPalette::Disabled;
648
QColor pen = palette().color(group,QPalette::ButtonText);
650
QPainter paint(this);
654
paint.setBrush(QBrush(_col));
655
paint.drawRect(5, 5, width()-10, height()-10);
657
paint.fillRect(5, 5, width()-10, height()-10, QBrush(_col));
661
// overpaint the rectangle to paint icon and text
662
QStyleOptionButton opt;
666
opt.iconSize = iconSize();
668
QStylePainter p(this);
669
p.drawControl(QStyle::CE_PushButtonLabel, opt);
672
void ColorButton::onChooseColor()
676
QColor c = QColorDialog::getColor( _col, this );
684
// ------------------------------------------------------------------------------
686
PrefColorButton::PrefColorButton ( QWidget * parent )
687
: ColorButton(parent)
691
PrefColorButton::~PrefColorButton()
695
QByteArray PrefColorButton::entryName () const
700
QByteArray PrefColorButton::paramGrpPath () const
705
void PrefColorButton::setEntryName ( const QByteArray& name )
710
void PrefColorButton::setParamGrpPath ( const QByteArray& name )
715
// --------------------------------------------------------------------
717
PrefLineEdit::PrefLineEdit ( QWidget * parent )
722
PrefLineEdit::~PrefLineEdit()
726
QByteArray PrefLineEdit::entryName () const
731
QByteArray PrefLineEdit::paramGrpPath () const
736
void PrefLineEdit::setEntryName ( const QByteArray& name )
741
void PrefLineEdit::setParamGrpPath ( const QByteArray& name )
746
// --------------------------------------------------------------------
748
PrefComboBox::PrefComboBox ( QWidget * parent )
754
PrefComboBox::~PrefComboBox()
758
QByteArray PrefComboBox::entryName () const
763
QByteArray PrefComboBox::paramGrpPath () const
768
void PrefComboBox::setEntryName ( const QByteArray& name )
773
void PrefComboBox::setParamGrpPath ( const QByteArray& name )
778
// --------------------------------------------------------------------
780
PrefCheckBox::PrefCheckBox ( QWidget * parent )
786
PrefCheckBox::~PrefCheckBox()
790
QByteArray PrefCheckBox::entryName () const
795
QByteArray PrefCheckBox::paramGrpPath () const
800
void PrefCheckBox::setEntryName ( const QByteArray& name )
805
void PrefCheckBox::setParamGrpPath ( const QByteArray& name )
810
// --------------------------------------------------------------------
812
PrefRadioButton::PrefRadioButton ( QWidget * parent )
813
: QRadioButton(parent)
815
setText("RadioButton");
818
PrefRadioButton::~PrefRadioButton()
822
QByteArray PrefRadioButton::entryName () const
827
QByteArray PrefRadioButton::paramGrpPath () const
832
void PrefRadioButton::setEntryName ( const QByteArray& name )
837
void PrefRadioButton::setParamGrpPath ( const QByteArray& name )
842
// --------------------------------------------------------------------
844
PrefSlider::PrefSlider ( QWidget * parent )
849
PrefSlider::~PrefSlider()
853
QByteArray PrefSlider::entryName () const
858
QByteArray PrefSlider::paramGrpPath () const
863
void PrefSlider::setEntryName ( const QByteArray& name )
868
void PrefSlider::setParamGrpPath ( const QByteArray& name )