QColorButton.h:
/*
* Copyright (C) 2008/07/02 Florian Becker <[email]fb@pyades.com[/email]>
*
* The ownership of this document rests with the Pyades Technologies GmbH.
* It is strictly prohibited to change, sell or publish it in any way. In
* case you have access to this document, you are obligated to ensure its
* nondisclosure. Noncompliances will be prosecuted.
*
* Diese Datei ist Eigentum der Pyades Technologies GmbH. Ändern, verkaufen
* oder auf eine andere Weise verbreiten und Öffentlich machen ist strikt
* untersagt. Falls Sie Zugang zu dieser Datei haben, sind Sie verpflichtet
* alles Mögliche für deren Geheimhaltung zu tun. Zuwiderhandlungen werden
* strafrechtlich verfolgt.
*/
#ifndef QCOLORBUTTON_H
#define QCOLORBUTTON_H
/* qt header */
#include <QPushButton>
/**
* Colorbutton,选择和显示实际的颜色.
*
* @brief Choose color via QPushButton and display it.
* @author Florian Becker (Pyades Technologies GmbH) <[email]fb@pyades.com[/email]>
* @date 2008/07/02
* @since 1.0.0
* @version 1.0.0
*/
class QColorButton : public QPushButton {
Q_OBJECT
Q_CLASSINFO( "author", "Florian Becker" )
Q_CLASSINFO( "email", "[email]fb@pyades.com[/email]" )
Q_CLASSINFO( "company", "Pyades Technologies GmbH" )
Q_CLASSINFO( "link", "http://www.pyades.com" )
Q_CLASSINFO( "date", "2008/07/02" )
public:
/**
* QColorButton的缺省构造函数.
*
* @param parent - 父级widget.
* @since 1.0.0
*/
QColorButton( QWidget *parent = 0 );
/**
* 设置当前颜色.
*
* @param color -要设置的颜色.
* @since 1.0.0
*/
void setCurrentColor( const QColor &color );
/**
* 返回当前颜色.
*
* @return 当前颜色.
* @since 1.0.0
*/
inline QColor currentColor() const { return mCurrentColor; }
/**
* 设置按钮文本.
*
* @param text - 要设置的文本.
* @since 1.0.0
*/
void setText( const QString &text );
/**
* Enable- 如果开启则显示用户定义的文本否则显示颜色值
*
* @param enableColorText - True,则显示用户定义的文本;否则false显示颜色值.
* 缺省为 true.
* @since 1.0.0
*/
void enableColorText( bool enableColorText );
/**
* 是否设置了用户定义文本还是颜色值?
*
* @如果设置用户定义文本则返回True;否则如果开启颜色值则返回false.
*
* @since 1.0.0
*/
inline bool isColorTextEnabled() const { return mEnableColorText; }
/**
* Enable-显示颜色值的ToolTip.
*
* @param enableToolTip - True, 开启显示颜色值的ToolTip - 没开启为false
* 缺省为true.
* @since 1.0.0
*/
void enableToolTip( bool enableToolTip );
/**
* 颜色值显示的tooltip是否开启?
*
* @return True,-如果开启 - 否则 false.
* @since 1.0.0
*/
inline bool isToolTipEnabled() const { return mEnableToolTip; }
private:
/**
* Intern function to set the new or changed color value as text.
*
* @param colorText The color value as text.
* @since 1.0.0
*/
void setColorText( const QString &colorText );
/**
*颜色文本是否开启?
*
* @since 1.0.0
*/
bool mEnableColorText;
/**
* 是否开启了color Tooltip?
*
* @since 1.0.0
*/
bool mEnableToolTip;
/**
* 当前颜色.
*
* @since 1.0.0
*/
QColor mCurrentColor;
/**
* 实际的文本.
*
* @since 1.0.0
*/
QString mRealText;
private slots:
/**
* 内部slot用来调用colordialog并修改颜色
*
* @since 1.0.0
*/
void slotChangeColor();
};
#endif
* Copyright (C) 2008/07/02 Florian Becker <[email]fb@pyades.com[/email]>
*
* The ownership of this document rests with the Pyades Technologies GmbH.
* It is strictly prohibited to change, sell or publish it in any way. In
* case you have access to this document, you are obligated to ensure its
* nondisclosure. Noncompliances will be prosecuted.
*
* Diese Datei ist Eigentum der Pyades Technologies GmbH. Ändern, verkaufen
* oder auf eine andere Weise verbreiten und Öffentlich machen ist strikt
* untersagt. Falls Sie Zugang zu dieser Datei haben, sind Sie verpflichtet
* alles Mögliche für deren Geheimhaltung zu tun. Zuwiderhandlungen werden
* strafrechtlich verfolgt.
*/
#ifndef QCOLORBUTTON_H
#define QCOLORBUTTON_H
/* qt header */
#include <QPushButton>
/**
* Colorbutton,选择和显示实际的颜色.
*
* @brief Choose color via QPushButton and display it.
* @author Florian Becker (Pyades Technologies GmbH) <[email]fb@pyades.com[/email]>
* @date 2008/07/02
* @since 1.0.0
* @version 1.0.0
*/
class QColorButton : public QPushButton {
Q_OBJECT
Q_CLASSINFO( "author", "Florian Becker" )
Q_CLASSINFO( "email", "[email]fb@pyades.com[/email]" )
Q_CLASSINFO( "company", "Pyades Technologies GmbH" )
Q_CLASSINFO( "link", "http://www.pyades.com" )
Q_CLASSINFO( "date", "2008/07/02" )
public:
/**
* QColorButton的缺省构造函数.
*
* @param parent - 父级widget.
* @since 1.0.0
*/
QColorButton( QWidget *parent = 0 );
/**
* 设置当前颜色.
*
* @param color -要设置的颜色.
* @since 1.0.0
*/
void setCurrentColor( const QColor &color );
/**
* 返回当前颜色.
*
* @return 当前颜色.
* @since 1.0.0
*/
inline QColor currentColor() const { return mCurrentColor; }
/**
* 设置按钮文本.
*
* @param text - 要设置的文本.
* @since 1.0.0
*/
void setText( const QString &text );
/**
* Enable- 如果开启则显示用户定义的文本否则显示颜色值
*
* @param enableColorText - True,则显示用户定义的文本;否则false显示颜色值.
* 缺省为 true.
* @since 1.0.0
*/
void enableColorText( bool enableColorText );
/**
* 是否设置了用户定义文本还是颜色值?
*
* @如果设置用户定义文本则返回True;否则如果开启颜色值则返回false.
*
* @since 1.0.0
*/
inline bool isColorTextEnabled() const { return mEnableColorText; }
/**
* Enable-显示颜色值的ToolTip.
*
* @param enableToolTip - True, 开启显示颜色值的ToolTip - 没开启为false
* 缺省为true.
* @since 1.0.0
*/
void enableToolTip( bool enableToolTip );
/**
* 颜色值显示的tooltip是否开启?
*
* @return True,-如果开启 - 否则 false.
* @since 1.0.0
*/
inline bool isToolTipEnabled() const { return mEnableToolTip; }
private:
/**
* Intern function to set the new or changed color value as text.
*
* @param colorText The color value as text.
* @since 1.0.0
*/
void setColorText( const QString &colorText );
/**
*颜色文本是否开启?
*
* @since 1.0.0
*/
bool mEnableColorText;
/**
* 是否开启了color Tooltip?
*
* @since 1.0.0
*/
bool mEnableToolTip;
/**
* 当前颜色.
*
* @since 1.0.0
*/
QColor mCurrentColor;
/**
* 实际的文本.
*
* @since 1.0.0
*/
QString mRealText;
private slots:
/**
* 内部slot用来调用colordialog并修改颜色
*
* @since 1.0.0
*/
void slotChangeColor();
};
#endif
QColorButton.cpp:
/*
* Copyright (C) 2008/07/02 Florian Becker <[email]fb@pyades.com[/email]>
*
* The ownership of this document rests with the Pyades Technologies GmbH.
* It is strictly prohibited to change, sell or publish it in any way. In
* case you have access to this document, you are obligated to ensure its
* nondisclosure. Noncompliances will be prosecuted.
*
* Diese Datei ist Eigentum der Pyades Technologies GmbH. Ändern, verkaufen
* oder auf eine andere Weise verbreiten und Öffentlich machen ist strikt
* untersagt. Falls Sie Zugang zu dieser Datei haben, sind Sie verpflichtet
* alles Mögliche für deren Geheimhaltung zu tun. Zuwiderhandlungen werden
* strafrechtlich verfolgt.
*/
/* qt header */
#include <QColorDialog>
/* local header */
#include "QColorButton.h"
QColorButton::QColorButton( QWidget *parent )
: QPushButton( parent ),
mEnableColorText( true ),
mEnableToolTip( true ) {
connect( this, SIGNAL( clicked() ), SLOT( slotChangeColor() ) ); //如果单击按钮执行slotChangeColor
setCurrentColor( "#ffffff" );
}
//设置当前颜色
void QColorButton::setCurrentColor( const QColor ¤tColor ) {
//如果没有改变颜色则返回
if ( mCurrentColor == currentColor )
return;
mCurrentColor = currentColor; //更新mCurrentColor变量
if ( mEnableColorText )
setColorText( mCurrentColor.name() ); //如#336699
if ( mEnableToolTip )
setToolTip( mCurrentColor.name() );
QPixmap pix( 16, 16 ); //16*16
pix.fill( mCurrentColor ); //用当前色填充
setIcon( pix ); //设置按钮图标
}
void QColorButton::setText( const QString &text ) {
if ( mRealText == text )
return;
mRealText = text;
QPushButton::setText( mRealText );
}
//开启颜色值文本
void QColorButton::enableColorText( bool enableColorText ) {
if ( mEnableColorText == enableColorText )
return;
mEnableColorText = enableColorText;
if ( mEnableColorText )
setColorText( mCurrentColor.name() );
else
setText( mRealText );
}
//开启tooltip
void QColorButton::enableToolTip( bool enableToolTip ) {
if ( mEnableToolTip == enableToolTip )
return;
mEnableToolTip = enableToolTip;
if ( mEnableToolTip )
setToolTip( mCurrentColor.name() ); //设置tooltip为颜色值
else
setToolTip( "" );
}
//设置颜色文本
void QColorButton::setColorText( const QString &colorText ) {
//如果文本没有改变则返回
if ( text() == colorText )
return;
QPushButton::setText( colorText ); //明确调用父类的方法
}
//自定义的slot
void QColorButton::slotChangeColor() {
//弹出颜色对话框,初始值设置为mCurrentColor
QColor newColor = QColorDialog::getColor( mCurrentColor, this );
if ( newColor.isValid() )
setCurrentColor( newColor ); //如果颜色值合法
}
* Copyright (C) 2008/07/02 Florian Becker <[email]fb@pyades.com[/email]>
*
* The ownership of this document rests with the Pyades Technologies GmbH.
* It is strictly prohibited to change, sell or publish it in any way. In
* case you have access to this document, you are obligated to ensure its
* nondisclosure. Noncompliances will be prosecuted.
*
* Diese Datei ist Eigentum der Pyades Technologies GmbH. Ändern, verkaufen
* oder auf eine andere Weise verbreiten und Öffentlich machen ist strikt
* untersagt. Falls Sie Zugang zu dieser Datei haben, sind Sie verpflichtet
* alles Mögliche für deren Geheimhaltung zu tun. Zuwiderhandlungen werden
* strafrechtlich verfolgt.
*/
/* qt header */
#include <QColorDialog>
/* local header */
#include "QColorButton.h"
QColorButton::QColorButton( QWidget *parent )
: QPushButton( parent ),
mEnableColorText( true ),
mEnableToolTip( true ) {
connect( this, SIGNAL( clicked() ), SLOT( slotChangeColor() ) ); //如果单击按钮执行slotChangeColor
setCurrentColor( "#ffffff" );
}
//设置当前颜色
void QColorButton::setCurrentColor( const QColor ¤tColor ) {
//如果没有改变颜色则返回
if ( mCurrentColor == currentColor )
return;
mCurrentColor = currentColor; //更新mCurrentColor变量
if ( mEnableColorText )
setColorText( mCurrentColor.name() ); //如#336699
if ( mEnableToolTip )
setToolTip( mCurrentColor.name() );
QPixmap pix( 16, 16 ); //16*16
pix.fill( mCurrentColor ); //用当前色填充
setIcon( pix ); //设置按钮图标
}
void QColorButton::setText( const QString &text ) {
if ( mRealText == text )
return;
mRealText = text;
QPushButton::setText( mRealText );
}
//开启颜色值文本
void QColorButton::enableColorText( bool enableColorText ) {
if ( mEnableColorText == enableColorText )
return;
mEnableColorText = enableColorText;
if ( mEnableColorText )
setColorText( mCurrentColor.name() );
else
setText( mRealText );
}
//开启tooltip
void QColorButton::enableToolTip( bool enableToolTip ) {
if ( mEnableToolTip == enableToolTip )
return;
mEnableToolTip = enableToolTip;
if ( mEnableToolTip )
setToolTip( mCurrentColor.name() ); //设置tooltip为颜色值
else
setToolTip( "" );
}
//设置颜色文本
void QColorButton::setColorText( const QString &colorText ) {
//如果文本没有改变则返回
if ( text() == colorText )
return;
QPushButton::setText( colorText ); //明确调用父类的方法
}
//自定义的slot
void QColorButton::slotChangeColor() {
//弹出颜色对话框,初始值设置为mCurrentColor
QColor newColor = QColorDialog::getColor( mCurrentColor, this );
if ( newColor.isValid() )
setCurrentColor( newColor ); //如果颜色值合法
}
本文转自 xcf007 51CTO博客,原文链接:
http://blog.51cto.com/xcf007/104778
,如需转载请自行联系原作者