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

kig

  • sources
  • kde-4.12
  • kdeedu
  • kig
  • modes
  • popup
builtinobjectactionsprovider.cc
Go to the documentation of this file.
1 
20 #include "builtinobjectactionsprovider.h"
21 
22 #include "popup.h"
23 #include "popupactionprovider.h"
24 
25 #include "../../kig/kig_commands.h"
26 #include "../../kig/kig_part.h"
27 #include "../../kig/kig_view.h"
28 #include "../../misc/kigpainter.h"
29 #include "../../objects/line_imp.h"
30 #include "../../modes/moving.h"
31 #include "../../modes/normal.h"
32 #include "../../objects/object_drawer.h"
33 #include "../../objects/point_imp.h"
34 
35 #include <QPen>
36 #include <QRect>
37 
38 #include <KColorDialog>
39 #include <KLocale>
40 
41 struct color_struct
42 {
43  const Qt::GlobalColor color;
44  const char* name;
45 };
46 
47 static const color_struct colors[] =
48 {
49  { Qt::black, I18N_NOOP( "Black" ) },
50  { Qt::gray, I18N_NOOP( "Gray" ) },
51  { Qt::red, I18N_NOOP( "Red" ) },
52  { Qt::green, I18N_NOOP( "Green" ) },
53  { Qt::cyan, I18N_NOOP( "Cyan" ) },
54  { Qt::yellow, I18N_NOOP( "Yellow" ) },
55  { Qt::darkRed, I18N_NOOP( "Dark Red" ) }
56 };
57 
58 const int numberofcolors = 7; // is there a better way to calc that?
59 
60 void BuiltinObjectActionsProvider::fillUpMenu( NormalModePopupObjects& popup, int menu, int& nextfree )
61 {
62  KIconLoader* l = popup.part().iconLoader();
63  if ( menu == NormalModePopupObjects::ToplevelMenu )
64  {
65  std::vector<ObjectHolder*> os = popup.objects();
66 
67  /*
68  * mp: we want the "show" action to be visible only
69  * if we selected only one object (to be conservative)
70  * and if that object is currently hidden.
71  * conversely for one hidden object we don't want
72  * the "hide" action to be inserted.
73  * in any case we have a fixed 'id' associated
74  * with the two actions.
75  */
76 
77  if ( os.size() > 1 || os.front()->shown() )
78  {
79  popup.addInternalAction( menu, i18n( "&Hide" ), nextfree );
80  }
81  if ( os.size() == 1 && !os.front()->shown() )
82  {
83  popup.addInternalAction( menu, i18n( "&Show" ), nextfree+1 );
84  }
85  nextfree += 2;
86  popup.addInternalAction( menu, KIcon( "transform-move", l ), i18n( "&Move" ), nextfree++ );
87  popup.addInternalAction( menu, KIcon( "edit-delete", l ), i18n( "&Delete" ), nextfree++ );
88  }
89  else if ( menu == NormalModePopupObjects::SetColorMenu )
90  {
91  QPixmap p( 20, 20 );
92  for( int i = 0; i < numberofcolors; i++ )
93  {
94  p.fill( QColor( colors[i].color ) );
95  popup.addInternalAction( menu, QIcon( p ), i18n( colors[i].name ), nextfree++ );
96  }
97  popup.addInternalAction( menu, KIcon( "color", l ), i18n( "&Custom Color" ), nextfree++ );
98  }
99  else if ( menu == NormalModePopupObjects::SetSizeMenu && !popup.onlyLabels() )
100  {
101  bool point = true;
102  bool samecolor = true;
103  std::vector<ObjectHolder*> os = popup.objects();
104  QColor color = os.front()->drawer()->color();
105  for ( std::vector<ObjectHolder*>::const_iterator i = os.begin(); i != os.end(); ++i )
106  {
107  if ( ! (*i)->imp()->inherits( PointImp::stype() ) )
108  point = false;
109  if ( (*i)->drawer()->color() != color ) samecolor = false;
110  };
111  if ( ! samecolor ) color = Qt::blue;
112  QPixmap p( 20, 20 );
113  for ( int i = 1; i < 8; ++i )
114  {
115  p.fill( popup.palette().color( popup.backgroundRole() ) );
116  QPainter ptr( &p );
117  ptr.setPen( QPen( color, 1 ) );
118  ptr.setBrush( QBrush( color, Qt::SolidPattern ) );
119  if ( point )
120  {
121  int size = 2*i;
122  QRect r( ( 20 - size ) / 2, ( 20 - size ) / 2, size, size );
123  ptr.drawEllipse( r );
124  }
125  else
126  {
127  ptr.setPen( QPen( color, -1 + 2*i ) );
128  ptr.drawLine( QPoint( 0, 10 ), QPoint( 50, 10 ) );
129  };
130  ptr.end();
131  popup.addInternalAction( menu, QIcon( p ), nextfree++ );
132  };
133  }
134  else if ( menu == NormalModePopupObjects::SetStyleMenu && !popup.onlyLabels() )
135  {
136  bool samecolor = true;
137  int npoints = 0;
138  int nothers = 0;
139  std::vector<ObjectHolder*> os = popup.objects();
140  QColor color = os.front()->drawer()->color();
141  for ( std::vector<ObjectHolder*>::const_iterator i = os.begin(); i != os.end(); ++i )
142  {
143  if ( (*i)->imp()->inherits( PointImp::stype() ) )
144  npoints++;
145  else
146  nothers++;
147  if ( (*i)->drawer()->color() != color ) samecolor = false;
148  };
149  bool point = ( npoints > nothers );
150  if ( ! samecolor ) color = Qt::blue;
151  if ( point )
152  for ( int i = 0; i < 5; ++i )
153  {
154  QPixmap p( 20, 20 );
155  p.fill( popup.palette().color( popup.backgroundRole() ) );
156  ScreenInfo si( Rect( -1, -1, 2, 2 ), p.rect() );
157  KigPainter ptr( si, &p, popup.part().document(), false );
158  PointImp pt( Coordinate( 0, 0 ) );
159  ObjectDrawer d( color, -1, true, Qt::SolidLine, i );
160  d.draw( pt, ptr, false );
161  popup.addInternalAction( menu, QIcon( p ), nextfree++ );
162  }
163  else
164  {
165  Qt::PenStyle penstyles[] = {Qt::SolidLine, Qt::DashLine, Qt::DashDotLine, Qt::DashDotDotLine, Qt::DotLine};
166  for ( int i = 0; i < (int) ( sizeof( penstyles ) / sizeof( Qt::PenStyle ) ); ++i )
167  {
168  QPixmap p( 20, 20 );
169  p.fill( popup.palette().color( popup.backgroundRole() ) );
170  ScreenInfo si( Rect( -2.5, -1, 2.5, 1 ), p.rect() );
171  KigPainter ptr( si, &p, popup.part().document(), false );
172  LineImp line( Coordinate( -1, 0 ), Coordinate( 1, 0 ) );
173  Qt::PenStyle ps = penstyles[i];
174  ObjectDrawer d( color, 2, true, ps, 1 );
175  d.draw( line, ptr, false );
176  popup.addInternalAction( menu, QIcon( p ), nextfree++ );
177  };
178  }
179  }
180 }
181 
182 bool BuiltinObjectActionsProvider::executeAction(
183  int menu, int& id, const std::vector<ObjectHolder*>& os, NormalModePopupObjects& popup,
184  KigPart& doc, KigWidget& w, NormalMode& mode )
185 {
186  if ( menu == NormalModePopupObjects::ToplevelMenu )
187  {
188  if ( id > 3 )
189  {
190  id -= 4;
191  return false;
192  };
193  switch( id )
194  {
195  case 0:
196  // hide the objects..
197  doc.hideObjects( os );
198  break;
199  case 1:
200  // show the objects..
201  doc.showObjects( os );
202  break;
203  case 2:
204  {
205  // move
206  QCursor::setPos( popup.mapToGlobal( QPoint( 0, 0 ) ) );
207  QPoint p = w.mapFromGlobal( QCursor::pos() );
208  Coordinate c = w.fromScreen( p );
209  MovingMode m( os, c, w, doc );
210  doc.runMode( &m );
211  // in this case, we return, cause we don't want objects to be
212  // unselected... ( or maybe we do ? )
213  return true;
214  }
215  case 3:
216  // delete
217  doc.delObjects( os );
218  break;
219  default: assert( false );
220  };
221  mode.clearSelection();
222  return true;
223  }
224  else if ( menu == NormalModePopupObjects::SetColorMenu )
225  {
226  if ( id >= numberofcolors + 1 )
227  {
228  id -= numberofcolors + 1;
229  return false;
230  };
231  QColor color;
232  if ( id < numberofcolors )
233  color = QColor( colors[id].color );
234  else
235  {
236  if ( os.size() == 1 )
237  color = os.front()->drawer()->color();
238  int result = KColorDialog::getColor( color, &w );
239  if ( result != KColorDialog::Accepted ) return true;
240  }
241  KigCommand* kc = new KigCommand( doc, i18n( "Change Object Color" ) );
242  assert( color.isValid() );
243  for ( std::vector<ObjectHolder*>::const_iterator i = os.begin(); i != os.end(); ++i )
244  kc->addTask( new ChangeObjectDrawerTask( *i, ( *i )->drawer()->getCopyColor( color ) ) );
245  doc.history()->push( kc );
246  return true;
247  }
248  else if ( menu == NormalModePopupObjects::SetSizeMenu )
249  {
250  if ( id >= 7 )
251  {
252  id -= 7;
253  return false;
254  };
255 
256  KigCommand* kc = new KigCommand( doc, i18n( "Change Object Width" ) );
257  for ( std::vector<ObjectHolder*>::const_iterator i = os.begin(); i != os.end(); ++i )
258  kc->addTask( new ChangeObjectDrawerTask( *i, ( *i )->drawer()->getCopyWidth( 1 + 2 * id ) ) );
259  doc.history()->push( kc );
260  return true;
261  }
262  else if ( menu == NormalModePopupObjects::SetStyleMenu )
263  {
264  int npoints = 0;
265  int nothers = 0;
266  for ( std::vector<ObjectHolder*>::const_iterator i = os.begin(); i != os.end(); ++i )
267  {
268  if ( (*i)->imp()->inherits( PointImp::stype() ) )
269  npoints++;
270  else
271  nothers++;
272  };
273  bool point = ( npoints > nothers );
274  int max = point ? 5 : 5;
275  if ( id >= max )
276  {
277  id -= max;
278  return false;
279  };
280 
281  if ( point )
282  {
283  KigCommand* kc = new KigCommand( doc, i18n( "Change Point Style" ) );
284  for ( std::vector<ObjectHolder*>::const_iterator i = os.begin(); i != os.end(); ++i )
285  if ( (*i)->imp()->inherits( PointImp::stype() ) )
286  kc->addTask( new ChangeObjectDrawerTask( *i, ( *i )->drawer()->getCopyPointStyle( id ) ) );
287  doc.history()->push( kc );
288  return true;
289  }
290  else
291  {
292  Qt::PenStyle penstyles[] = {Qt::SolidLine, Qt::DashLine, Qt::DashDotLine, Qt::DashDotDotLine, Qt::DotLine};
293  assert( id < (int)( sizeof( penstyles ) / sizeof( Qt::PenStyle ) ) );
294  Qt::PenStyle p = penstyles[id];
295  KigCommand* kc = new KigCommand( doc, i18n( "Change Object Style" ) );
296  for ( std::vector<ObjectHolder*>::const_iterator i = os.begin(); i != os.end(); ++i )
297  if ( ! (*i)->imp()->inherits( PointImp::stype() ) )
298  kc->addTask( new ChangeObjectDrawerTask( *i, ( *i )->drawer()->getCopyStyle( p ) ) );
299  doc.history()->push( kc );
300  }
301  return true;
302  }
303  else return false;
304 }
305 
306 
popup.h
NormalModePopupObjects::part
KigPart & part()
Definition: popup.h:70
KigCommand
a KigCommand represents almost every action performed in Kig.
Definition: kig_commands.h:44
NormalModePopupObjects::addInternalAction
QAction * addInternalAction(int menu, const QString &name, int id)
Definition: popup.cc:276
NormalModePopupObjects::SetSizeMenu
Definition: popup.h:60
KigCommand::addTask
void addTask(KigCommandTask *)
Definition: kig_commands.cpp:74
Rect
This file is part of Kig, a KDE program for Interactive Geometry...
Definition: rect.h:34
KigPart::document
const KigDocument & document() const
Definition: kig_part.cpp:989
NormalModePopupObjects::ToplevelMenu
Definition: popup.h:60
KigPart::history
QUndoStack * history()
Definition: kig_part.cpp:633
KigPart::hideObjects
void hideObjects(const std::vector< ObjectHolder * > &os)
Definition: kig_part.cpp:937
ObjectDrawer::draw
void draw(const ObjectImp &imp, KigPainter &p, bool selected) const
Draw the object imp on kigpainter p .
Definition: object_drawer.cc:29
colors
static const color_struct colors[]
Definition: builtinobjectactionsprovider.cc:47
NormalModePopupObjects::SetStyleMenu
Definition: popup.h:60
ScreenInfo
ScreenInfo is a simple utility class that maps a region of the document onto a region of the screen...
Definition: screeninfo.h:31
Coordinate
The Coordinate class is the basic class representing a 2D location by its x and y components...
Definition: coordinate.h:33
NormalModePopupObjects
This is the popup menu that appears when you click on selected objects in NormalMode.
Definition: popup.h:46
ChangeObjectDrawerTask
Definition: kig_commands.h:223
MovingMode
Definition: moving.h:73
KigPainter
KigPainter is an extended QPainter.
Definition: kigpainter.h:51
PointImp
An ObjectImp representing a point.
Definition: point_imp.h:27
KigWidget::fromScreen
const Coordinate fromScreen(const QPoint &p)
Definition: kig_view.cpp:282
KigPart::showObjects
void showObjects(const std::vector< ObjectHolder * > &os)
Definition: kig_part.cpp:956
BuiltinObjectActionsProvider::fillUpMenu
void fillUpMenu(NormalModePopupObjects &popup, int menu, int &nextfree)
add all your entries to menu menu in popup popup.
Definition: builtinobjectactionsprovider.cc:60
KigPart::runMode
void runMode(KigMode *)
Definition: kig_part.cpp:735
KigWidget
This class is the real widget showing the document.
Definition: kig_view.h:50
popupactionprovider.h
builtinobjectactionsprovider.h
numberofcolors
const int numberofcolors
Definition: builtinobjectactionsprovider.cc:58
NormalModePopupObjects::objects
std::vector< ObjectHolder * > objects() const
Definition: popup.h:69
NormalModePopupObjects::onlyLabels
bool onlyLabels() const
Definition: popup.h:74
ObjectDrawer
A class holding some information about how a certain object is drawn on the window.
Definition: object_drawer.h:47
PointImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing PointImp's.
Definition: point_imp.cc:159
BuiltinObjectActionsProvider::executeAction
bool executeAction(int menu, int &id, const std::vector< ObjectHolder * > &os, NormalModePopupObjects &popup, KigPart &doc, KigWidget &w, NormalMode &m)
try to execute the id'th action you added to menu menu in popup popup ( first is 0 )...
Definition: builtinobjectactionsprovider.cc:182
NormalMode::clearSelection
void clearSelection()
Definition: normal.cc:88
LineImp
An ObjectImp representing a line.
Definition: line_imp.h:184
KigPart
This is a "Part".
Definition: kig_part.h:68
NormalModePopupObjects::SetColorMenu
Definition: popup.h:60
KigPart::delObjects
void delObjects(const std::vector< ObjectHolder * > &os)
Definition: kig_part.cpp:638
NormalMode
Definition: normal.h:26
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:39 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kig

Skip menu "kig"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

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