📄 kcolordlg.cpp
字号:
label = new QLabel( "B:", this ); label->setMinimumSize(label->sizeHint()); label->setAlignment(AlignRight | AlignVCenter); l_rbot->addWidget(label, 2, 4); bedit = new QLineEdit( this ); bedit->setFixedHeight(bedit->sizeHint().height()); l_rbot->addWidget(bedit, 2, 5); connect( bedit, SIGNAL( returnPressed() ), SLOT( slotRGBChanged() ) ); // the entry fields should be wide enought to hold 88888 int w = hedit->fontMetrics().boundingRect("88888").width(); hedit->setMinimumWidth(w); redit->setMinimumWidth(w); // the label rows should not expand l_rbot->setColStretch(2, 0); l_rbot->setColStretch(4, 0); // the entry rows should expand l_rbot->setColStretch(3, 1); l_rbot->setColStretch(5, 1); // a little separator between KSeparator *sep = new KSeparator(this); tl_layout->addMultiCellWidget(sep, 1, 1, 0, 2); // the standard buttons KButtonBox *bbox = new KButtonBox(this); button = bbox->addButton(klocale->translate("Help")); connect( button, SIGNAL(clicked()), SLOT(getHelp())); bbox->addStretch(1); button = bbox->addButton(klocale->translate("OK")); connect( button, SIGNAL( clicked() ), SLOT( slotOkPressed() ) ); button = bbox->addButton(klocale->translate("Cancel")); connect( button, SIGNAL( clicked() ), SLOT( reject() ) ); bbox->layout(); bbox->setMinimumSize(bbox->sizeHint()); tl_layout->addMultiCellWidget(bbox, 2, 2, 0, 2); tl_layout->setRowStretch(0, 1); tl_layout->setRowStretch(1, 0); tl_layout->setRowStretch(2, 0); tl_layout->activate(); tl_layout->freeze(); readSettings(); setRgbEdit(); setHsvEdit(); palette->setValues( h, s ); valuePal->setValue( v );}void KColorDialog::setColor( const QColor &col ){ selColor = col; setRgbEdit(); setHsvEdit(); int h, s, v; selColor.hsv( &h, &s, &v ); palette->setValues( h, s ); valuePal->setHue( h ); valuePal->setSaturation( s ); valuePal->drawPalette(); valuePal->repaint( FALSE ); valuePal->setValue( v ); patch->setColor( selColor );}// static function to display dialog and return colorint KColorDialog::getColor( QColor &theColor ){ KColorDialog dlg( 0L, "Color Selector", TRUE ); if ( theColor.isValid() ) dlg.setColor( theColor ); int result = dlg.exec(); if ( result == Accepted ) theColor = dlg.color(); return result;}void KColorDialog::slotOkPressed(){ writeSettings(); accept();}void KColorDialog::slotRGBChanged(){ int red = atoi( redit->text() ); int grn = atoi( gedit->text() ); int blu = atoi( bedit->text() ); if ( red > 255 || red < 0 ) return; if ( grn > 255 || grn < 0 ) return; if ( blu > 255 || blu < 0 ) return; selColor.setRgb( red, grn, blu ); patch->setColor( selColor ); setRgbEdit(); setHsvEdit(); int h, s, v; selColor.hsv( &h, &s, &v ); palette->setValues( h, s ); valuePal->setHue( h ); valuePal->setSaturation( s ); valuePal->drawPalette(); valuePal->repaint( FALSE ); valuePal->setValue( v ); emit colorSelected( selColor );}void KColorDialog::slotHSVChanged(){ int hue = atoi( hedit->text() ); int sat = atoi( sedit->text() ); int val = atoi( vedit->text() ); if ( hue > 359 || hue < 0 ) return; if ( sat > 255 || sat < 0 ) return; if ( val > 255 || val < 0 ) return; selColor.setHsv( hue, sat, val ); patch->setColor( selColor ); setRgbEdit(); setHsvEdit(); palette->setValues( hue, sat ); valuePal->setHue( hue ); valuePal->setSaturation( sat ); valuePal->drawPalette(); valuePal->repaint( FALSE ); valuePal->setValue( val ); emit colorSelected( selColor );}void KColorDialog::slotHSChanged( int h, int s ){ selColor.setHsv( h, s, valuePal->value() ); valuePal->setHue( h ); valuePal->setSaturation( s ); valuePal->drawPalette(); valuePal->repaint( FALSE ); patch->setColor( selColor ); setRgbEdit(); setHsvEdit(); emit colorSelected( selColor );}void KColorDialog::slotVChanged( int v ){ selColor.setHsv( palette->xValue(), palette->yValue(), v ); patch->setColor( selColor ); setRgbEdit(); setHsvEdit(); emit colorSelected( selColor );}void KColorDialog::slotSysColorSelected( int col ){ selColor = sysColorCells->color( col ); patch->setColor( selColor ); setRgbEdit(); setHsvEdit(); int h, s, v; selColor.hsv( &h, &s, &v ); palette->setValues( h, s ); valuePal->setHue( h ); valuePal->setSaturation( s ); valuePal->drawPalette(); valuePal->repaint( FALSE ); valuePal->setValue( v ); emit colorSelected( selColor );}void KColorDialog::slotCustColorSelected( int col ){ QColor color = custColorCells->color( col ); // if a color has not been assigned to this cell, don't change current col if ( color == lightGray ) return; selColor = color; patch->setColor( selColor ); setRgbEdit(); setHsvEdit(); int h, s, v; selColor.hsv( &h, &s, &v ); palette->setValues( h, s ); valuePal->setHue( h ); valuePal->setSaturation( s ); valuePal->drawPalette(); valuePal->repaint( FALSE ); valuePal->setValue( v ); emit colorSelected( selColor );}void KColorDialog::slotAddToCustom(){ custColorCells->setColor( custColorCells->getSelected(), selColor );}void KColorDialog::readSettings(){ QColor col; QString key; KConfig* config = kapp->getConfig(); QString oldgroup = config->group(); config->setGroup( "Custom Colors"); for ( int i = 0; i < custColorCells->numCells(); i++ ) { key.sprintf( "Color%d", i ); col = config->readColorEntry( key, &lightGray ); custColorCells->setColor( i, col ); } config->setGroup( oldgroup );}void KColorDialog::writeSettings(){ QColor color; QString key; KConfig* config = kapp->getConfig(); QString oldgroup = config->group(); config->setGroup( "Custom Colors"); for ( int i = 0; i < custColorCells->numCells(); i++ ) { color = custColorCells->color( i ); key.sprintf( "Color%d", i ); config->writeEntry( key, color, true, true ); } config->setGroup( oldgroup );}void KColorDialog::setRgbEdit(){ int r, g, b; selColor.rgb( &r, &g, &b ); QString num; num.setNum( r ); redit->setText( num ); num.setNum( g ); gedit->setText( num ); num.setNum( b ); bedit->setText( num );}void KColorDialog::setHsvEdit(){ int h, s, v; selColor.hsv( &h, &s, &v ); QString num; num.setNum( h ); hedit->setText( num ); num.setNum( s ); sedit->setText( num ); num.setNum( v ); vedit->setText( num );}void KColorDialog::getHelp() { if(kapp != 0) kapp->invokeHTMLHelp("kcolordialog.html", "");}//----------------------------------------------------------------------------KColorCombo::KColorCombo( QWidget *parent, const char *name ) : QComboBox( parent, name ){ customColor.setRgb( 255, 255, 255 ); color.setRgb( 255, 255, 255 ); createStandardPalette(); addColors(); connect( this, SIGNAL( activated(int) ), SLOT( slotActivated(int) ) ); connect( this, SIGNAL( highlighted(int) ), SLOT( slotHighlighted(int) ) );}void KColorCombo::setColor( const QColor &col ){ color = col; addColors();}void KColorCombo::resizeEvent( QResizeEvent *re ){ QComboBox::resizeEvent( re ); addColors();}void KColorCombo::slotActivated( int index ){ if ( index == 0 ) { if ( KColorDialog::getColor( customColor ) == QDialog::Accepted ) { QRect rect( 0, 0, width(), 20 ); QPixmap pixmap( rect.width(), rect.height() ); QPainter painter; QPen pen; if ( qGray( customColor.rgb() ) < 128 ) pen.setColor( white ); else pen.setColor( black ); painter.begin( &pixmap ); QBrush brush( customColor ); painter.fillRect( rect, brush ); painter.setPen( pen ); painter.drawText( 2, 18, klocale->translate("Custom...") ); painter.end(); changeItem( pixmap, 0 ); pixmap.detach(); } color = customColor; } else color = standardPalette[ index - 1 ]; emit activated( color );}void KColorCombo::slotHighlighted( int index ){ if ( index == 0 ) color = customColor; else color = standardPalette[ index - 1 ]; emit highlighted( color );}void KColorCombo::addColors(){ QRect rect( 0, 0, width(), 20 ); QPixmap pixmap( rect.width(), rect.height() ); QPainter painter; QPen pen; int i; clear(); createStandardPalette(); for ( i = 0; i < STANDARD_PAL_SIZE; i++ ) if ( standardPalette[i] == color ) break; if ( i == STANDARD_PAL_SIZE ) customColor = color; if ( qGray( customColor.rgb() ) < 128 ) pen.setColor( white ); else pen.setColor( black ); painter.begin( &pixmap ); QBrush brush( customColor ); painter.fillRect( rect, brush ); painter.setPen( pen ); painter.drawText( 2, 18, klocale->translate("Custom...") ); painter.end(); insertItem( pixmap ); pixmap.detach(); for ( i = 0; i < STANDARD_PAL_SIZE; i++ ) { painter.begin( &pixmap ); QBrush brush( standardPalette[i] ); painter.fillRect( rect, brush ); painter.end(); insertItem( pixmap ); pixmap.detach(); if ( standardPalette[i] == color ) setCurrentItem( i + 1 ); }}#include "kcolordlg.moc"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -