forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,107 @@ | ||
#include "Button.h" | ||
|
||
Button::Button(QWidget *parent) : | ||
QPushButton(parent) | ||
, mState(NotFocused) | ||
{ | ||
} | ||
|
||
Button::Button(const QString& text, QWidget *parent) : | ||
QPushButton(text, parent) | ||
, mState(NotFocused) | ||
{ | ||
} | ||
|
||
Button::~Button() | ||
{ | ||
} | ||
|
||
Button::IconState Button::iconState() const | ||
{ | ||
return mState; | ||
} | ||
|
||
void Button::setIconState(IconState state, bool force) | ||
{ | ||
if (mState == state && !force) | ||
return; | ||
mState = state; | ||
setIcon(iconForState(mState)); | ||
} | ||
|
||
QIcon Button::iconForState(IconState state) const | ||
{ | ||
if (!mIcons.contains(state)) | ||
return mIcons.begin().value(); | ||
return mIcons.value(state);; | ||
} | ||
|
||
void Button::setIconForState(IconState state, const QIcon &icon) | ||
{ | ||
mIcons.insert(state, icon); | ||
} | ||
|
||
void Button::setIconWithSates(const QPixmap &pixmap, IconState s1, IconState s2, IconState s3, IconState s4) | ||
{ | ||
int a = pixmap.width(); | ||
int b = pixmap.height(); | ||
int count = qMax(a, b)/qMin(a, b); | ||
bool hor = pixmap.height() < pixmap.width(); | ||
QPoint dp; | ||
if (hor) { | ||
a /= count; | ||
dp.setX(a); | ||
} else { | ||
b /= count; | ||
dp.setY(b); | ||
} | ||
QRect r(0, 0, a, b); | ||
setIconForState(s1, pixmap.copy(r)); | ||
if (count > 1) { | ||
r.translate(dp); | ||
setIconForState(s2, pixmap.copy(r)); | ||
if (count > 2) { | ||
r.translate(dp); | ||
setIconForState(s3, pixmap.copy(r)); | ||
if (count > 3) { | ||
r.translate(dp); | ||
setIconForState(s4, pixmap.copy(r)); | ||
} | ||
} | ||
} | ||
setIconState(iconState(), true); //TODO: other states set to existing icon | ||
} | ||
|
||
void Button::enterEvent(QEvent *e) | ||
{ | ||
QPushButton::enterEvent(e); | ||
if (mIcons.isEmpty()) | ||
return; | ||
setIconState(Focused); | ||
emit entered(); | ||
} | ||
|
||
void Button::leaveEvent(QEvent *e) | ||
{ | ||
QPushButton::leaveEvent(e); | ||
if (mIcons.isEmpty()) | ||
return; | ||
setIconState(NotFocused); | ||
emit leaved(); | ||
} | ||
|
||
void Button::mousePressEvent(QMouseEvent *e) | ||
{ | ||
QPushButton::mousePressEvent(e); | ||
if (mIcons.isEmpty()) | ||
return; | ||
setIconState(Pressed); | ||
} | ||
|
||
void Button::mouseReleaseEvent(QMouseEvent *e) | ||
{ | ||
QPushButton::mouseReleaseEvent(e); | ||
if (mIcons.isEmpty()) | ||
return; | ||
setIconState(Focused); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,48 @@ | ||
#ifndef BUTTON_H | ||
#define BUTTON_H | ||
|
||
//TODO: on/off, like qicon | ||
|
||
#include <QPushButton> | ||
#include <QMap> | ||
|
||
class Button : public QPushButton | ||
{ | ||
Q_OBJECT | ||
public: | ||
//like QIcon::Mode | ||
//use icon state to avoid setIcon with the same icon | ||
enum IconState { | ||
Disabled, //inactive | ||
Pressed, //focused and pressed | ||
Focused, //mouse enter | ||
NotFocused //mouse leave | ||
}; | ||
|
||
explicit Button(QWidget *parent = 0); | ||
explicit Button(const QString& text, QWidget *parent = 0); | ||
virtual ~Button(); | ||
IconState iconState() const; | ||
void setIconState(IconState state, bool force = false); | ||
QIcon iconForState(IconState state) const; | ||
void setIconForState(IconState state, const QIcon& icon); | ||
|
||
void setIconWithSates(const QPixmap& pixmap, IconState s1 = NotFocused, IconState s2 = Disabled | ||
, IconState s3 = Pressed, IconState s4 = Focused); | ||
signals: | ||
void entered(); | ||
void leaved(); | ||
|
||
protected: | ||
//for change icon. | ||
virtual void enterEvent(QEvent *); | ||
virtual void leaveEvent(QEvent *); | ||
virtual void mousePressEvent(QMouseEvent *e); | ||
virtual void mouseReleaseEvent(QMouseEvent *e); | ||
|
||
private: | ||
IconState mState; | ||
QMap<IconState, QIcon> mIcons; | ||
}; | ||
|
||
#endif // BUTTON_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters