Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vtabbutton.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006 by Gopala Krishna A <krishna.ggk@gmail.com> *
3  * *
4  * This is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2, or (at your option) *
7  * any later version. *
8  * *
9  * This software is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License *
15  * along with this package; see the file COPYING. If not, write to *
16  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, *
17  * Boston, MA 02110-1301, USA. *
18  ***************************************************************************/
19 
20 #include "vtabbutton.h"
21 #include <QString>
22 #include <QStyle>
23 #include <QPainter>
24 #include <QApplication>
25  #include <QStyleOption>
26 #include <QStylePainter>
27 
28 VTab::VTab(VTabPosition pos,int p_id,QWidget *p,const char* n) : QPushButton(p,n)
29 {
30  m_position = pos;
31  m_id = p_id;
32  init();
33 }
34 
35 void VTab::init()
36 {
37  setFlat(true);
38  setToggleButton(true);
39  setAutoDefault(false);
40  setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
41  connect(this,SIGNAL(toggled(bool )),SLOT(slotToggled(bool)));
42 }
43 
45 {}
46 
47 void VTab::setID(int p_id)
48 {
49  m_id = p_id;
50 }
51 
53 {
54  if(m_position != p_pos)
55  {
56  m_position = p_pos;
57  repaint();
58  }
59 }
60 
61 
62 QSize VTab::sizeHint() const
63 {
64  constPolish();
65  QString _text = text();
66  if(_text.isNull())
67  _text = "XXX";
68  QFontMetrics fm = this->fontMetrics();
69  int w = fm.height()+5;
70  int h = 24+fm.width(_text);
71  QStyleOption *so= new QStyleOption();
72  so->initFrom(this);
73  QSize sz=style()->sizeFromContents(
74  QStyle::CT_ToolButton,
75  so, QSize(w, h).expandedTo(QApplication::globalStrut()));
76  return sz;
77 }
78 
79 void VTab::drawButton(QPainter *p)
80 {
81  p->save();
82  QStyle::State st = QStyle::State_None | QStyle::State_Enabled;
83  if (isOn()) {
84  st |= QStyle::State_On;
85  st |= QStyle::State_DownArrow;
86  }
87 
88  QRect r(0, 0, height(), width());
89 
90  if (m_position == TabLeft) {
91  p->translate(0, height());
92  p->rotate(-90);
93  }
94  else {
95  p->translate(width(), 0);
96  p->rotate(90);
97  }
98 QStyleOption *so= new QStyleOption();
99  so->initFrom(this);
100  style()->drawControl(QStyle::CE_PushButton, so,p);//, r);//, colorGroup(), st);
101  style()->drawControl(QStyle::CE_PushButtonLabel, so,p);//, colorGroup(), st);
102 
103  p->restore();
104 }
105 
106 void VTab::setText(const QString &s)
107 {
108  QPushButton::setText(s);
109  const QSize sz = sizeHint();
110  setFixedHeight(sz.height());
111  setFixedWidth(sz.width());
112  updateGeometry();
113 }
114 
115 void VTab::slotToggled(bool b)
116 {
117  emit toggled(m_id,b);
118 }
119 
120 void VTab::paintEvent(QPaintEvent* event)
121 {
122  Q_UNUSED(event);
123  QStylePainter p(this);
124 
125  p.rotate(-90);
126  p.translate(-height(), 0);
127  //p.rotate(90);
128  //p.translate(0, -width());
129 
130  p.drawControl(QStyle::CE_PushButton, getStyleOption());
131 }
132 
133 QStyleOptionButton VTab::getStyleOption() const
134 {
135  QStyleOptionButton opt;
136  opt.initFrom(this);
137 
138  QSize size = opt.rect.size();
139  size.transpose();
140  opt.rect.setSize(size);
141 
142  opt.features = QStyleOptionButton::None;
143  if (isFlat())
144  opt.features |= QStyleOptionButton::Flat;
145  if (menu())
146  opt.features |= QStyleOptionButton::HasMenu;
147  if (autoDefault() || isDefault())
148  opt.features |= QStyleOptionButton::AutoDefaultButton;
149  if (isDefault())
150  opt.features |= QStyleOptionButton::DefaultButton;
151  if (isDown() )
152  opt.state |= QStyle::State_Sunken;
153  if (isChecked())
154  opt.state |= QStyle::State_On;
155  if (!isFlat() && !isDown())
156  opt.state |= QStyle::State_Raised;
157  opt.text = text();
158  opt.icon = icon();
159  opt.iconSize = iconSize();
160  return opt;
161 }
QStyleOptionButton getStyleOption() const
Definition: vtabbutton.cpp:133
void slotToggled(bool b)
Definition: vtabbutton.cpp:115
virtual QSize sizeHint() const
Definition: vtabbutton.cpp:62
void setID(int p_id)
Definition: vtabbutton.cpp:47
virtual void setText(const QString &s)
Definition: vtabbutton.cpp:106
void paintEvent(QPaintEvent *event)
Definition: vtabbutton.cpp:120
void setPosition(VTabPosition p_pos)
Definition: vtabbutton.cpp:52
virtual ~VTab()
Definition: vtabbutton.cpp:44
VTab(VTabPosition pos=TabLeft, int p_id=-1, QWidget *parent=0l, const char *name=0l)
Definition: vtabbutton.cpp:28
int m_id
Definition: vtabbutton.h:58
VTabPosition m_position
Definition: vtabbutton.h:57
void init()
Definition: vtabbutton.cpp:35
virtual void drawButton(QPainter *p)
Definition: vtabbutton.cpp:79
void toggled(int p_id, bool p_state)