#pragma once#include <QtGui/QAbstractButton>#include <QtDesigner/QDesignerExportWidget>#include "signature_lib.h"class QMenu; class SIGNATURECOLLECTION_EXPORT SignatureButton : public QAbstractButton{ Q_OBJECT Q_PROPERTY(QIcon normal_state_icon READ GetNormalStateIcon WRITE SetNormalStateIcon) Q_PROPERTY(QIcon active_state_icon READ GetActiveStateIcon WRITE SetActiveStateIcon)public: SignatureButton(QWidget *parent = 0); ~SignatureButton(); QSize sizeHint() const; void SetMenu(QMenu* i_menu); void ShowMenu() const; void AddActions(QList<QAction*> i_actions); //////////////////////////////////////////////// void SetNormalStateIcon(const QIcon& i_icon) { if(m_normal_state_icon.isNull()) { m_normal_state_icon = i_icon; setIcon(m_normal_state_icon); } return; } void SetActiveStateIcon(const QIcon& i_icon) { if(m_active_state_icon.isNull()) m_active_state_icon = i_icon; return; } const QIcon& GetNormalStateIcon() { return m_normal_state_icon; } const QIcon& GetActiveStateIcon() { return m_active_state_icon; } bool IsActive() const; ///////////////////////////////////////////////public slots: void ChangeState();protected: void paintEvent(QPaintEvent *e) void mouseReleaseEvent(QMouseEvent* e); void mousePressEvent(QMouseEvent* e);//todo check for needing of this event method void enterEvent(QEvent*); void leaveEvent(QEvent*);private: bool m_is_active; QIcon m_normal_state_icon; QIcon m_active_state_icon; QMenu* mp_menu;signals: void mousePressed(); void mouseReleased(); };
#include "signaturebutton.h"#include <QMenu>#include <QPainter>#include <QtCore>#include <QObject>#include <QMouseEvent>#include <QImage>#include <QtGui/QMessageBox>SignatureButton::SignatureButton(QWidget *parent) : QAbstractButton(parent), m_is_active(false), mp_menu(NULL) { setStyleSheet("font-family: Verdana;" "font-size: 12px;" "font-weight: bold;" ); }SignatureButton::~SignatureButton() {}QSize SignatureButton::sizeHint() const { if(icon().isNull()) return QSize(fontMetrics().width(text()) + 10, fontMetrics().height() + 10); else return icon().actualSize(QSize(300, 100)); }void SignatureButton::SetMenu(QMenu* i_menu) { mp_menu = i_menu; }void SignatureButton::ShowMenu() const { QPoint button_global_point = parentWidget()->mapToGlobal(QPoint(pos().x(), pos().y())); QPoint menu_global_point = QPoint(button_global_point.x(), button_global_point.y() + frameGeometry().height()); mp_menu->popup(menu_global_point); }void SignatureButton::AddActions(QList<QAction*> i_actions) { if(!i_actions.isEmpty()) mp_menu->addActions(i_actions); }bool SignatureButton::IsActive() const { return m_is_active; }void SignatureButton::ChangeState() { m_is_active = !m_is_active; }/////////////////////////////////////////////////////////////void SignatureButton::paintEvent(QPaintEvent* e)//todo - check { QPainter painter(this); if(m_is_active && !icon().isNull() && m_normal_state_icon.name() == icon().name()) setIcon(m_active_state_icon); else if(!m_is_active && !icon().isNull() && icon().name() == m_active_state_icon.name()) setIcon(m_normal_state_icon); icon().paint(&painter, rect(), Qt::AlignCenter, QIcon::Mode::Normal, QIcon::State::On); if(!text().isEmpty()) painter.drawText( rect(), Qt::AlignCenter, text()); }void SignatureButton::enterEvent(QEvent*) { m_is_active = true; }void SignatureButton::leaveEvent(QEvent*) { m_is_active = false; }void SignatureButton::mouseReleaseEvent(QMouseEvent* e) { if(e->button() == Qt::LeftButton) { m_is_active = false; emit mouseReleased();//todo check repaint(rect()); if(mp_menu) ShowMenu(); } }void SignatureButton::mousePressEvent(QMouseEvent* e) { emit mousePressed(); }
#pragma once#include <QtGui/QWidget>#include <QtCore/QtGlobal>#include "editbox.h"#include "signature_lib.h"#define EBID_MINUS 1#define EBID_PLUS 2class QHBoxLayout;class QSignalMapper;class EditBox;class QResizeEvent;class SIGNATURECOLLECTION_EXPORT SignatureEditBox : public QWidget { Q_OBJECT Q_PROPERTY(QIcon minus_normal_state_icon READ GetMinusNormalStateIcon WRITE SetMinusNormalStateIcon) Q_PROPERTY(QIcon minus_active_state_icon READ GetMinusActiveStateIcon WRITE SetMinusActiveStateIcon) Q_PROPERTY(QIcon edit_normal_state_icon READ GetEditNormalStateIcon WRITE SetEditNormalStateIcon) Q_PROPERTY(QIcon edit_active_state_icon READ GetEditActiveStateIcon WRITE SetEditActiveStateIcon) Q_PROPERTY(QIcon plus_normal_state_icon READ GetPlusNormalStateIcon WRITE SetPlusNormalStateIcon) Q_PROPERTY(QIcon plus_active_state_icon READ GetPlusActiveStateIcon WRITE SetPlusActiveStateIcon) Q_PROPERTY(QString text READ GetText WRITE SetText) public: SignatureEditBox(QWidget *parent = 0); ~SignatureEditBox(); //accessors for Q_PROPERTY(...) void SetMinusNormalStateIcon(const QIcon& i_icon) {mp_minus->SetNormalStateIcon(i_icon);} void SetMinusActiveStateIcon(const QIcon& i_icon) {mp_minus->SetActiveStateIcon(i_icon);} void SetEditNormalStateIcon(const QIcon& i_icon) {mp_editbox->SetNormalStateIcon(i_icon);} void SetEditActiveStateIcon(const QIcon& i_icon) {mp_editbox->SetActiveStateIcon(i_icon);} void SetPlusNormalStateIcon(const QIcon& i_icon) {mp_plus->SetNormalStateIcon(i_icon);} void SetPlusActiveStateIcon(const QIcon& i_icon) {mp_plus->SetActiveStateIcon(i_icon);} const QIcon& GetMinusNormalStateIcon() {return mp_minus->GetNormalStateIcon();} const QIcon& GetMinusActiveStateIcon() {return mp_minus->GetActiveStateIcon();} const QIcon& GetEditNormalStateIcon() {return mp_editbox->GetNormalStateIcon();} const QIcon& GetEditActiveStateIcon() {return mp_editbox->GetActiveStateIcon();} const QIcon& GetPlusNormalStateIcon() {return mp_plus->GetNormalStateIcon();} const QIcon& GetPlusActiveStateIcon() {return mp_plus->GetActiveStateIcon();} //////////////////////////////////////////////////// void SetText(const QString& i_text) {mp_editbox->setText(i_text);} const QString GetText() const {return mp_editbox->text();} QSize sizeHint() const;//todo reimplement void SetEditBoxText(const QString& i_text); void Compose(); protected: void resizeEvent(QResizeEvent*); private: SignatureButton* mp_minus; EditBox* mp_editbox; SignatureButton* mp_plus; QSignalMapper* mp_signal_press_mapper; QSignalMapper* mp_signal_release_mapper; public slots: void ChangeEditState(int i_button_id); };
#include "signatureeditbox.h"#include <QHBoxLayout>#include <QSignalMapper>#include <QString>#include <QPainter>#include <QResizeEvent>SignatureEditBox::SignatureEditBox(QWidget *parent) : QWidget(parent), mp_minus(new SignatureButton), mp_editbox(new EditBox), mp_plus(new SignatureButton), mp_signal_press_mapper(new QSignalMapper), mp_signal_release_mapper(new QSignalMapper) { mp_minus->setParent(this); mp_editbox->setParent(this); mp_plus->setParent(this); //signal mapping block mp_signal_release_mapper->setParent(this); mp_signal_release_mapper->setMapping(mp_minus, EBID_MINUS); mp_signal_release_mapper->setMapping(mp_plus, EBID_PLUS); connect(mp_minus, SIGNAL(mouseReleased()), mp_signal_release_mapper, SLOT(map())); connect(mp_plus, SIGNAL(mouseReleased()), mp_signal_release_mapper, SLOT(map())); mp_signal_press_mapper->setParent(this); mp_signal_press_mapper->setMapping(mp_minus, EBID_MINUS); mp_signal_press_mapper->setMapping(mp_plus, EBID_PLUS); connect(mp_minus, SIGNAL(mousePressed()), mp_signal_press_mapper, SLOT(map())); connect(mp_plus, SIGNAL(mousePressed()), mp_signal_press_mapper, SLOT(map())); connect(mp_signal_press_mapper, SIGNAL(mapped(int)), this, SLOT(ChangeEditState(int))); connect(mp_signal_release_mapper, SIGNAL(mapped(int)), this, SLOT(ChangeEditState(int))); }QSize SignatureEditBox::sizeHint() const { return QSize(mp_minus->sizeHint().width() + mp_editbox->sizeHint().width() + mp_plus->sizeHint().width() + 10, mp_plus->sizeHint().height()); }//in application window: after loading all icons - call resize() for SignatureEditBoxvoid SignatureEditBox::resizeEvent(QResizeEvent* ip_event) { //sizeHint using implementation int pure_edit_width = ip_event->size().width() - 10; int pure_edit_height = ip_event->size().height() - 5;//5 - top distance to components int width = pure_edit_width * mp_minus->sizeHint().width() / (sizeHint().width() - 10); int a = mp_minus->sizeHint().height(); int b = (sizeHint().height() - 5); int height = pure_edit_height * a / b; int offset = 0; mp_minus->setGeometry(offset += 3, mp_minus->y(), width, height); width = pure_edit_width * mp_editbox->sizeHint().width() / (sizeHint().width() - 10); height = pure_edit_height * mp_editbox->sizeHint().height() / (sizeHint().height() - 5); mp_editbox->setGeometry(offset += mp_minus->width() + 2, mp_editbox->y(), width, height); width = pure_edit_width * mp_plus->sizeHint().width() / (sizeHint().width() - 10); height = pure_edit_height * mp_plus->sizeHint().height() / (sizeHint().height() - 5); mp_plus->setGeometry(offset += mp_editbox->width() + 2, mp_plus->y(), width, height); }/////////////////////////////////////////////////////////////////////void SignatureEditBox::SetEditBoxText(const QString& i_text) { mp_editbox->setText(i_text); }SignatureEditBox::~SignatureEditBox() {}void SignatureEditBox::ChangeEditState(int i_button_id) { if(!mp_editbox->IsActive() && !mp_editbox->text().isNull()) { QStringList list = mp_editbox->text().split(" "); QString val_str = list.at(0); double val = val_str.toDouble(); QString str = list.at(1); switch(i_button_id) { case EBID_MINUS: mp_editbox->setText(QString::number((double)(val - 0.5)) + " " + str); break; case EBID_PLUS: mp_editbox->setText(QString::number((double)(val + 0.5)) + " " + str); break; } } mp_editbox->ChangeState(); update(mp_editbox->rect()); }
void SignatureButton::SetActive(bool i_state) { m_is_active = i_state; if(m_is_active && !icon().isNull() && m_normal_state_icon.name() == icon().name()) setIcon(m_active_state_icon); else if(!m_is_active && !icon().isNull() && icon().name() == m_active_state_icon.name()) setIcon(m_normal_state_icon); }void SignatureButton::paintEvent(QPaintEvent* e)//todo - check { QPainter painter(this); icon().paint(&painter, rect(), Qt::AlignCenter, QIcon::Mode::Normal, QIcon::State::On); if(!text().isEmpty()) painter.drawText( rect(), Qt::AlignCenter, text()); }