custom push button

This commit is contained in:
Marc Zinnschlag 2014-07-10 11:03:55 +02:00
parent 930f782bc5
commit dd0aa20390
5 changed files with 66 additions and 6 deletions

View file

@ -69,7 +69,7 @@ opencs_units_noqt (view/world
) )
opencs_units (view/widget opencs_units (view/widget
scenetoolbar scenetool scenetoolmode scenetoolbar scenetool scenetoolmode pushbutton
) )
opencs_units (view/render opencs_units (view/render

View file

@ -0,0 +1,29 @@
#include "pushbutton.hpp"
#include <QMouseEvent>
#include <QKeyEvent>
void CSVWidget::PushButton::keyPressEvent (QKeyEvent *event)
{
if (event->key()!=Qt::Key_Shift)
mKeepOpen = false;
QPushButton::keyPressEvent (event);
}
void CSVWidget::PushButton::mouseReleaseEvent (QMouseEvent *event)
{
mKeepOpen = event->button()==Qt::LeftButton && (event->modifiers() & Qt::ShiftModifier);
QPushButton::mouseReleaseEvent (event);
}
CSVWidget::PushButton::PushButton (const QIcon& icon, const QString& text, QWidget *parent)
: QPushButton (icon, text, parent), mKeepOpen (false)
{
}
bool CSVWidget::PushButton::hasKeepOpen() const
{
return mKeepOpen;
}

View file

@ -0,0 +1,28 @@
#ifndef CSV_WIDGET_PUSHBUTTON_H
#define CSV_WIDGET_PUSHBUTTON_H
#include <QPushButton>
namespace CSVWidget
{
class PushButton : public QPushButton
{
Q_OBJECT
bool mKeepOpen;
protected:
virtual void keyPressEvent (QKeyEvent *event);
virtual void mouseReleaseEvent (QMouseEvent *event);
public:
PushButton (const QIcon& icon, const QString& text, QWidget *parent = 0);
bool hasKeepOpen() const;
};
}
#endif

View file

@ -6,6 +6,7 @@
#include <QSignalMapper> #include <QSignalMapper>
#include "scenetoolbar.hpp" #include "scenetoolbar.hpp"
#include "pushbutton.hpp"
CSVWidget::SceneToolMode::SceneToolMode (SceneToolbar *parent) CSVWidget::SceneToolMode::SceneToolMode (SceneToolbar *parent)
: SceneTool (parent), mButtonSize (parent->getButtonSize()), mIconSize (parent->getIconSize()) : SceneTool (parent), mButtonSize (parent->getButtonSize()), mIconSize (parent->getIconSize())
@ -27,7 +28,7 @@ void CSVWidget::SceneToolMode::showPanel (const QPoint& position)
void CSVWidget::SceneToolMode::addButton (const std::string& icon, const std::string& id) void CSVWidget::SceneToolMode::addButton (const std::string& icon, const std::string& id)
{ {
QPushButton *button = new QPushButton (QIcon (QPixmap (icon.c_str())), "", mPanel); PushButton *button = new PushButton (QIcon (QPixmap (icon.c_str())), "", mPanel);
button->setSizePolicy (QSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed)); button->setSizePolicy (QSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed));
button->setIconSize (QSize (mIconSize, mIconSize)); button->setIconSize (QSize (mIconSize, mIconSize));
button->setFixedSize (mButtonSize, mButtonSize); button->setFixedSize (mButtonSize, mButtonSize);
@ -44,12 +45,13 @@ void CSVWidget::SceneToolMode::addButton (const std::string& icon, const std::st
void CSVWidget::SceneToolMode::selected() void CSVWidget::SceneToolMode::selected()
{ {
std::map<QPushButton *, std::string>::const_iterator iter = std::map<PushButton *, std::string>::const_iterator iter =
mButtons.find (dynamic_cast<QPushButton *> (sender())); mButtons.find (dynamic_cast<PushButton *> (sender()));
if (iter!=mButtons.end()) if (iter!=mButtons.end())
{ {
mPanel->hide(); if (!iter->first->hasKeepOpen())
mPanel->hide();
setIcon (iter->first->icon()); setIcon (iter->first->icon());
emit modeChanged (iter->second); emit modeChanged (iter->second);

View file

@ -10,6 +10,7 @@ class QHBoxLayout;
namespace CSVWidget namespace CSVWidget
{ {
class SceneToolbar; class SceneToolbar;
class PushButton;
///< \brief Mode selector tool ///< \brief Mode selector tool
class SceneToolMode : public SceneTool class SceneToolMode : public SceneTool
@ -18,7 +19,7 @@ namespace CSVWidget
QWidget *mPanel; QWidget *mPanel;
QHBoxLayout *mLayout; QHBoxLayout *mLayout;
std::map<QPushButton *, std::string> mButtons; // widget, id std::map<PushButton *, std::string> mButtons; // widget, id
int mButtonSize; int mButtonSize;
int mIconSize; int mIconSize;