• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kopete/kopete

  • sources
  • kde-4.14
  • kdenetwork
  • kopete
  • kopete
  • config
  • appearance
  • layout
TokenWithLayout.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2009 Nikolaj Hald Nielsen <nhnFreespirit@gmail.com> *
3  * Copyright (c) 2009 Roman Jarosz <kedgedev@gmail.com> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #include "TokenWithLayout.h"
22 
23 #include "TokenDropTarget.h"
24 
25 #include <KAction>
26 #include <KHBox>
27 #include <KLocale>
28 
29 #include <QActionGroup>
30 #include <QContextMenuEvent>
31 #include <QLayout>
32 #include <QMenu>
33 #include <QSlider>
34 #include <QLCDNumber>
35 
36 const QString ActionBoldName = QLatin1String( "ActionBold" );
37 const QString ActionItalicName = QLatin1String( "ActionItalic" );
38 const QString ActionAlignLeftName = QLatin1String( "ActionAlignLeft" );
39 const QString ActionAlignCenterName = QLatin1String( "ActionAlignCenter" );
40 const QString ActionAlignRightName = QLatin1String( "ActionAlignRight" );
41 
42 Token * TokenWithLayoutFactory::createToken(const QString &text, const QString &iconName, int value, QWidget *parent)
43 {
44  return new TokenWithLayout( text, iconName, value, parent );
45 }
46 
47 TokenWithLayout::TokenWithLayout( const QString &text, const QString &iconName, int value, QWidget *parent )
48  : Token( text, iconName, value, parent )
49  , m_width( 0.0 )
50 {
51  m_widthForced = m_width > 0.0;
52  m_alignment = Qt::AlignCenter;
53  m_bold = false;
54  m_italic = false;
55  setFocusPolicy( Qt::ClickFocus );
56 }
57 
58 
59 TokenWithLayout::~TokenWithLayout()
60 {
61 }
62 
63 void TokenWithLayout::fillMenu( QMenu * menu )
64 {
65  KAction *boldAction = new KAction( KIcon( "format-text-bold"), i18n( "Bold" ), menu );
66  boldAction->setObjectName( ActionBoldName );
67  boldAction->setCheckable( true );
68  boldAction->setChecked( m_bold );
69 
70  KAction *italicAction = new KAction( KIcon( "format-text-italic"), i18n( "Italic" ), menu );
71  italicAction->setObjectName( ActionItalicName );
72  italicAction->setCheckable( true );
73  italicAction->setChecked( m_italic );
74 
75  KAction *alignLeftAction = new KAction( KIcon( "format-justify-left"), i18n( "Left" ), menu );
76  KAction *alignCenterAction = new KAction( KIcon( "format-justify-center"), i18n( "Center" ), menu );
77  KAction *alignRightAction = new KAction( KIcon( "format-justify-right"), i18n( "Right" ), menu );
78  alignLeftAction->setObjectName( ActionAlignLeftName );
79  alignLeftAction->setCheckable( true );
80  alignCenterAction->setObjectName( ActionAlignCenterName );
81  alignCenterAction->setCheckable( true );
82  alignRightAction->setObjectName( ActionAlignRightName );
83  alignRightAction->setCheckable( true );
84 
85  if ( m_alignment & Qt::AlignLeft )
86  alignLeftAction->setChecked( true );
87  else if ( m_alignment & Qt::AlignHCenter )
88  alignCenterAction->setChecked( true );
89  else if ( m_alignment & Qt::AlignRight )
90  alignRightAction->setChecked( true );
91 
92  QActionGroup *alignmentGroup = new QActionGroup( menu );
93  alignmentGroup->addAction( alignLeftAction );
94  alignmentGroup->addAction( alignCenterAction );
95  alignmentGroup->addAction( alignRightAction );
96 
97  menu->addAction( boldAction );
98  menu->addAction( italicAction );
99  menu->addSeparator()->setText( i18n( "Alignment" ) );
100  menu->addAction( alignLeftAction );
101  menu->addAction( alignCenterAction );
102  menu->addAction( alignRightAction );
103  menu->addSeparator()->setText( i18n( "Width" ) );
104  menu->adjustSize();
105 
106  int orgHeight = menu->height();
107 
108  KHBox * sliderBox = new KHBox( menu );
109  sliderBox->setFixedWidth( menu->width() - 4 );
110  sliderBox->move( sliderBox->pos().x() + 2, orgHeight );
111 
112  QSlider * slider = new QSlider( Qt::Horizontal, sliderBox );
113  slider->setMaximum( 100 );
114  slider->setMinimum( 0 );
115 
116  // this should really not be done here as it makes upward assumptions
117  // it was however done in setWidth with similar upward assumptions as well
118  // solution: the popup stuff -iff- should be done in the dialog or the editWidget
119  if ( parentWidget() )
120  {
121  if ( TokenDropTarget *editWidget = qobject_cast<TokenDropTarget*>( parentWidget() ) )
122  {
123  qreal spareWidth = 100.0;
124  int row = editWidget->row( this );
125  if ( row > -1 )
126  {
127  QList<Token*> tokens = editWidget->drags( row );
128  foreach (Token *t, tokens)
129  {
130  if (t == this)
131  continue;
132  if ( TokenWithLayout *twl = qobject_cast<TokenWithLayout*>( t ) )
133  spareWidth -= twl->width() * 100.0;
134  }
135  }
136  slider->setMaximum( qMax<qreal>( spareWidth, 0.0 ) );
137  }
138  }
139  slider->setValue( m_width * 100.0 );
140 
141  QLCDNumber * sizeLabel = new QLCDNumber( 3, sliderBox );
142  sizeLabel->display( m_width * 100.0 );
143 
144  connect( slider, SIGNAL(valueChanged(int)), sizeLabel, SLOT(display(int)) );
145  connect( slider, SIGNAL(valueChanged(int)), this, SLOT(setWidth(int)) );
146 
147  menu->setFixedHeight( orgHeight + slider->height() );
148 
149  slider->setFixedWidth( menu->width() - 4 );
150 }
151 
152 void TokenWithLayout::menuExecuted( const QAction* action )
153 {
154  if( action->objectName() == ActionAlignLeftName )
155  setAlignment( Qt::AlignLeft );
156  else if( action->objectName() == ActionAlignCenterName )
157  setAlignment( Qt::AlignCenter );
158  else if( action->objectName() == ActionAlignRightName )
159  setAlignment( Qt::AlignRight );
160  else if( action->objectName() == ActionBoldName )
161  setBold( action->isChecked() );
162  else if( action->objectName() == ActionItalicName )
163  setItalic( action->isChecked() );
164 }
165 
166 void TokenWithLayout::contextMenuEvent( QContextMenuEvent * event )
167 {
168  QMenu* menu = new QMenu();
169 
170  menu->setTitle( i18n( "Layout" ) );
171 
172  fillMenu( menu );
173 
174  QAction* action = menu->exec( mapToGlobal( event->pos() ) );
175  if ( action )
176  menuExecuted( action );
177 
178  delete menu;
179 }
180 
181 Qt::Alignment TokenWithLayout::alignment()
182 {
183  return m_alignment;
184 }
185 
186 void TokenWithLayout::setAlignment( Qt::Alignment alignment )
187 {
188  if ( m_alignment == alignment )
189  return;
190 
191  m_alignment = alignment;
192  m_label->setAlignment( alignment );
193  emit changed();
194 }
195 
196 void TokenWithLayout::setAlignLeft( bool b )
197 {
198  if (b)
199  setAlignment( Qt::AlignLeft );
200 }
201 
202 void TokenWithLayout::setAlignCenter( bool b )
203 {
204  if (b)
205  setAlignment( Qt::AlignCenter );
206 }
207 
208 void TokenWithLayout::setAlignRight( bool b )
209 {
210  if (b)
211  setAlignment( Qt::AlignRight );
212 }
213 
214 bool TokenWithLayout::bold() const
215 {
216  return m_bold;
217 }
218 
219 void TokenWithLayout::setBold( bool bold )
220 {
221  if ( m_bold == bold )
222  return;
223 
224  m_bold = bold;
225  QFont font = m_label->font();
226  font.setBold( bold );
227  m_label->setFont( font );
228  emit changed();
229 }
230 
231 void TokenWithLayout::setPrefix( const QString& string )
232 {
233  if ( m_prefix == string )
234  return;
235 
236  m_prefix = string;
237  emit changed();
238 }
239 
240 void TokenWithLayout::setSuffix( const QString& string )
241 {
242  if ( m_suffix == string )
243  return;
244 
245  m_suffix = string;
246  emit changed();
247 }
248 
249 void TokenWithLayout::setWidth( int size )
250 {
251  m_width = qMax( qMin( 1.0, size/100.0 ), 0.0 ) ;
252  if ( (m_width) > 0.0 )
253  m_widthForced = true;
254 
255  emit changed();
256 }
257 
258 void TokenWithLayout::setWidthForced( bool on )
259 {
260  m_widthForced = on;
261 }
262 
263 qreal TokenWithLayout::width() const
264 {
265  return m_width;
266 }
267 
268 bool TokenWithLayout::italic() const
269 {
270  return m_italic;
271 }
272 
273 void TokenWithLayout::setItalic( bool italic )
274 {
275  if ( m_italic == italic )
276  return;
277 
278  m_italic = italic;
279  QFont font = m_label->font();
280  font.setItalic( italic );
281  m_label->setFont( font );
282 
283  emit changed();
284 }
285 
286 
287 #include "TokenWithLayout.moc"
288 
289 
290 
TokenWithLayout.h
QAction::setText
void setText(const QString &text)
QWidget
TokenWithLayout::setAlignCenter
void setAlignCenter(bool)
Definition: TokenWithLayout.cpp:202
QActionGroup
TokenWithLayout
An extended Token with controls for layouting the token and getting layout values for use outside the...
Definition: TokenWithLayout.h:38
QWidget::setFixedWidth
void setFixedWidth(int w)
TokenWithLayout::TokenWithLayout
TokenWithLayout(const QString &text, const QString &iconName, int value, QWidget *parent=0)
Definition: TokenWithLayout.cpp:47
Token::changed
void changed()
TokenWithLayout::setBold
void setBold(bool bold)
Definition: TokenWithLayout.cpp:219
QWidget::setFocusPolicy
void setFocusPolicy(Qt::FocusPolicy policy)
QAction::isChecked
bool isChecked() const
QFont
QMenu::addAction
void addAction(QAction *action)
Token::m_label
QLabel * m_label
Definition: Token.h:65
ActionItalicName
const QString ActionItalicName
Definition: TokenWithLayout.cpp:37
QWidget::mapToGlobal
QPoint mapToGlobal(const QPoint &pos) const
QLabel::setAlignment
void setAlignment(QFlags< Qt::AlignmentFlag >)
TokenWithLayout::width
qreal width() const
Definition: TokenWithLayout.cpp:263
QActionGroup::addAction
QAction * addAction(QAction *action)
TokenDropTarget
Definition: TokenDropTarget.h:32
Qt::Alignment
typedef Alignment
QWidget::adjustSize
void adjustSize()
ActionBoldName
const QString ActionBoldName
Definition: TokenWithLayout.cpp:36
QSlider
TokenWithLayout::bold
bool bold() const
Definition: TokenWithLayout.cpp:214
QWidget::width
width
QFont::setBold
void setBold(bool enable)
TokenWithLayout::fillMenu
virtual void fillMenu(QMenu *menu)
Definition: TokenWithLayout.cpp:63
QContextMenuEvent
TokenWithLayout::setWidth
void setWidth(int width)
Definition: TokenWithLayout.cpp:249
ActionAlignCenterName
const QString ActionAlignCenterName
Definition: TokenWithLayout.cpp:39
QObject::objectName
objectName
ActionAlignRightName
const QString ActionAlignRightName
Definition: TokenWithLayout.cpp:40
TokenWithLayout::alignment
Qt::Alignment alignment()
Definition: TokenWithLayout.cpp:181
QMenu::setTitle
void setTitle(const QString &title)
TokenWithLayout::setWidthForced
void setWidthForced(bool)
Definition: TokenWithLayout.cpp:258
QMenu::addSeparator
QAction * addSeparator()
QString
QList
QAbstractSlider::setMinimum
void setMinimum(int)
TokenDropTarget.h
QMenu::exec
QAction * exec()
QMenu
TokenWithLayout::setItalic
void setItalic(bool italic)
Definition: TokenWithLayout.cpp:273
QWidget::font
const QFont & font() const
QAbstractSlider::setValue
void setValue(int)
QFont::setItalic
void setItalic(bool enable)
TokenWithLayout::contextMenuEvent
virtual void contextMenuEvent(QContextMenuEvent *event)
Definition: TokenWithLayout.cpp:166
TokenWithLayout::italic
bool italic() const
Definition: TokenWithLayout.cpp:268
TokenWithLayout::setAlignRight
void setAlignRight(bool)
Definition: TokenWithLayout.cpp:208
QContextMenuEvent::pos
const QPoint & pos() const
TokenWithLayoutFactory::createToken
virtual Token * createToken(const QString &text, const QString &iconName, int value, QWidget *parent=0)
Definition: TokenWithLayout.cpp:42
QLatin1String
QWidget::setFixedHeight
void setFixedHeight(int h)
TokenWithLayout::~TokenWithLayout
~TokenWithLayout()
Definition: TokenWithLayout.cpp:59
QWidget::parentWidget
QWidget * parentWidget() const
QAction
KAction
QLCDNumber::display
void display(const QString &s)
TokenWithLayout::setSuffix
void setSuffix(const QString &)
Definition: TokenWithLayout.cpp:240
TokenWithLayout::menuExecuted
virtual void menuExecuted(const QAction *action)
Definition: TokenWithLayout.cpp:152
TokenWithLayout::setPrefix
void setPrefix(const QString &)
Definition: TokenWithLayout.cpp:231
QLCDNumber
Token
Definition: Token.h:38
QAbstractSlider::setMaximum
void setMaximum(int)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
TokenWithLayout::setAlignment
void setAlignment(Qt::Alignment alignment)
Definition: TokenWithLayout.cpp:186
TokenWithLayout::setAlignLeft
void setAlignLeft(bool)
Definition: TokenWithLayout.cpp:196
QWidget::height
height
ActionAlignLeftName
const QString ActionAlignLeftName
Definition: TokenWithLayout.cpp:38
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/kopete

Skip menu "kopete/kopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal