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

kig

  • sources
  • kde-4.14
  • kdeedu
  • kig
  • objects
text_type.cc
Go to the documentation of this file.
1 // Copyright (C) 2003 Dominique Devriese <devriese@kde.org>
2 
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 // 02110-1301, USA.
17 
18 #include "text_type.h"
19 
20 #include "text_imp.h"
21 #include "bogus_imp.h"
22 #include "object_drawer.h"
23 #include "point_imp.h"
24 #include "line_imp.h"
25 
26 #include "../kig/kig_view.h"
27 #include "../kig/kig_part.h"
28 #include "../kig/kig_commands.h"
29 #include "../modes/label.h"
30 #include "../misc/coordinate_system.h"
31 
32 #include <algorithm>
33 
34 #include <qapplication.h>
35 #include <qclipboard.h>
36 #include <qstringlist.h>
37 
38 #include <kfontdialog.h>
39 
40 #include <cmath>
41 #include <iterator>
42 
43 static const ArgsParser::spec arggspeccs[] =
44 {
45  { IntImp::stype(), "UNUSED", "SHOULD NOT BE SEEN", false },
46  { PointImp::stype(), "UNUSED", "SHOULD NOT BE SEEN", false },
47  { StringImp::stype(), "UNUSED", "SHOULD NOT BE SEEN", false }
48 };
49 
50 // KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( GenericTextType )
51 
52 GenericTextType::GenericTextType( const char fulltypename[] )
53  : ObjectType( fulltypename ), mparser( arggspeccs, 3 )
54 {
55 }
56 
57 GenericTextType::~GenericTextType()
58 {
59 }
60 
61 const ObjectImpType* GenericTextType::resultId() const
62 {
63  return TextImp::stype();
64 }
65 
66 const ObjectImpType* GenericTextType::impRequirement( const ObjectImp* o, const Args& args ) const
67 {
68  assert( args.size() >= 3 );
69  Args firstthree( args.begin(), args.begin() + 3 );
70  if ( o == args[0] || o == args[1] || o == args[2] )
71  return argParser().impRequirement( o, firstthree );
72  else
73  return ObjectImp::stype();
74 }
75 
76 ObjectImp* GenericTextType::calc( const Args& parents, const KigDocument& doc ) const
77 {
78  if( parents.size() < 3 ) return new InvalidImp;
79  Args firstthree( parents.begin(), parents.begin() + 3 );
80  Args varargs( parents.begin() + 3, parents.end() );
81 
82  if ( ! mparser.checkArgs( firstthree ) ) return new InvalidImp;
83 
84  int frame = static_cast<const IntImp*>( firstthree[0] )->data();
85  bool needframe = frame != 0;
86  const Coordinate t = static_cast<const PointImp*>( firstthree[1] )->coordinate();
87  QString s = static_cast<const StringImp*>( firstthree[2] )->data();
88 
89  for ( Args::iterator i = varargs.begin(); i != varargs.end(); ++i )
90  (*i)->fillInNextEscape( s, doc );
91 
92  if ( varargs.size() == 1 && varargs[0]->inherits( DoubleImp::stype() ) )
93  {
94  double value = static_cast<const DoubleImp*>( varargs[0] )->data();
95  return new NumericTextImp( s, t, needframe, value );
96  } else if ( varargs.size() == 1 && varargs[0]->inherits( TestResultImp::stype() ) )
97  {
98  bool value = static_cast<const TestResultImp*>( varargs[0] )->truth();
99  return new BoolTextImp( s, t, needframe, value );
100  } else {
101  return new TextImp( s, t, needframe );
102  }
103 }
104 
105 bool GenericTextType::canMove( const ObjectTypeCalcer& ) const
106 {
107  return true;
108 }
109 
110 bool GenericTextType::isFreelyTranslatable( const ObjectTypeCalcer& ) const
111 {
112  return true;
113 }
114 
115 void GenericTextType::move( ObjectTypeCalcer& ourobj, const Coordinate& to,
116  const KigDocument& d ) const
117 {
118  const std::vector<ObjectCalcer*> parents = ourobj.parents();
119  assert( parents.size() >= 3 );
120  const std::vector<ObjectCalcer*> firstthree( parents.begin(), parents.begin() + 3 );
121  if( dynamic_cast<ObjectConstCalcer*>( firstthree[1] ) )
122  {
123  ObjectConstCalcer* c = static_cast<ObjectConstCalcer*>( firstthree[1] );
124  c->setImp( new PointImp( to ) );
125  }
126  else
127  firstthree[1]->move( to, d );
128 }
129 
130 const ArgsParser& GenericTextType::argParser() const
131 {
132  return mparser;
133 }
134 
135 const Coordinate GenericTextType::moveReferencePoint( const ObjectTypeCalcer& ourobj ) const
136 {
137  assert( ourobj.imp()->inherits( TextImp::stype() ) );
138  return static_cast<const TextImp*>( ourobj.imp() )->coordinate();
139 }
140 
141 std::vector<ObjectCalcer*> GenericTextType::sortArgs( const std::vector<ObjectCalcer*>& os ) const
142 {
143  assert( os.size() >= 3 );
144  std::vector<ObjectCalcer*> ret( os.begin(), os.begin() + 3 );
145  ret = mparser.parse( ret );
146  std::copy( os.begin() + 3, os.end(), std::back_inserter( ret ) );
147  return ret;
148 }
149 
150 Args GenericTextType::sortArgs( const Args& args ) const
151 {
152  assert( args.size() >= 3 );
153  Args ret( args.begin(), args.begin() + 3 );
154  ret = mparser.parse( ret );
155  std::copy( args.begin() + 3, args.end(), std::back_inserter( ret ) );
156  return ret;
157 }
158 
159 std::vector<ObjectCalcer*> GenericTextType::movableParents( const ObjectTypeCalcer& ourobj ) const
160 {
161  const std::vector<ObjectCalcer*> parents = ourobj.parents();
162  assert( parents.size() >= 3 );
163  std::vector<ObjectCalcer*> ret = parents[1]->movableParents();
164  ret.push_back( parents[1] );
165  return ret;
166 }
167 
168 bool GenericTextType::isDefinedOnOrThrough( const ObjectImp*, const Args& ) const
169 {
170  return false;
171 }
172 
173 QStringList GenericTextType::specialActions() const
174 {
175  QStringList ret;
176  ret << i18n( "&Copy Text" );
177  ret << i18n( "&Toggle Frame" );
178  ret << i18n( "Set &Font..." );
179  return ret;
180 }
181 
182 void GenericTextType::executeAction( int i, ObjectHolder& oh, ObjectTypeCalcer& c,
183  KigPart& doc, KigWidget& w,
184  NormalMode& ) const
185 {
186  std::vector<ObjectCalcer*> parents = c.parents();
187  assert( parents.size() >= 3 );
188 
189  std::vector<ObjectCalcer*> firstthree( parents.begin(), parents.begin() + 3 );
190 
191  assert( mparser.checkArgs( firstthree ) );
192  assert( dynamic_cast<ObjectConstCalcer*>( firstthree[0] ) );
193  assert( dynamic_cast<ObjectConstCalcer*>( firstthree[2] ) );
194 
195  if ( i == 0 )
196  {
197  QClipboard* cb = QApplication::clipboard();
198 
199  // copy the text into the clipboard
200  const TextImp* ti = static_cast<const TextImp*>( c.imp() );
201  cb->setText( ti->text(), QClipboard::Clipboard );
202  }
203  else if ( i == 1 )
204  {
205  // toggle label frame
206  int n = (static_cast<const IntImp*>( firstthree[0]->imp() )->data() + 1) % 2;
207  KigCommand* kc = new KigCommand( doc, i18n( "Toggle Label Frame" ) );
208  kc->addTask( new ChangeObjectConstCalcerTask(
209  static_cast<ObjectConstCalcer*>( firstthree[0] ),
210  new IntImp( n ) ) );
211  doc.history()->push( kc );
212  }
213  else if ( i == 2 )
214  {
215  // change label font
216  QFont f = oh.drawer()->font();
217  int result = KFontDialog::getFont( f, KFontChooser::NoDisplayFlags, &w );
218  if ( result != KFontDialog::Accepted ) return;
219  KigCommand* kc = new KigCommand( doc, i18n( "Change Label Font" ) );
220  kc->addTask( new ChangeObjectDrawerTask( &oh, oh.drawer()->getCopyFont( f ) ) );
221  doc.history()->push( kc );
222  }
223  else assert( false );
224 }
225 
226 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( TextType )
227 
228 TextType::TextType()
229  : GenericTextType( "Label" )
230 {
231 }
232 
233 TextType::~TextType()
234 {
235 }
236 
237 const TextType* TextType::instance()
238 {
239  static const TextType t;
240  return &t;
241 }
242 
243 QStringList TextType::specialActions() const
244 {
245  QStringList ret = GenericTextType::specialActions();
246  ret << i18n( "&Redefine..." );
247  return ret;
248 }
249 
250 void TextType::executeAction( int i, ObjectHolder& o, ObjectTypeCalcer& c,
251  KigPart& doc, KigWidget& w,
252  NormalMode& nm ) const
253 {
254  std::vector<ObjectCalcer*> parents = c.parents();
255  assert( parents.size() >= 3 );
256 
257  std::vector<ObjectCalcer*> firstthree( parents.begin(), parents.begin() + 3 );
258 
259  assert( argParser().checkArgs( firstthree ) );
260  assert( dynamic_cast<ObjectConstCalcer*>( firstthree[0] ) );
261  assert( dynamic_cast<ObjectConstCalcer*>( firstthree[2] ) );
262 
263  const int parentactions = GenericTextType::specialActions().count();
264  if ( i < parentactions )
265  GenericTextType::executeAction( i, o, c, doc, w, nm );
266  else if ( i == parentactions )
267  {
268  assert( dynamic_cast<ObjectTypeCalcer*>( o.calcer() ) );
269  // redefine..
270  TextLabelRedefineMode m( doc, static_cast<ObjectTypeCalcer*>( o.calcer() ) );
271  doc.runMode( &m );
272  }
273  else assert( false );
274 }
275 
276 
277 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( NumericTextType )
278 
279 NumericTextType::NumericTextType()
280  : GenericTextType( "NumericLabel" )
281 {
282 }
283 
284 NumericTextType::~NumericTextType()
285 {
286 }
287 
288 const NumericTextType* NumericTextType::instance()
289 {
290  static const NumericTextType t;
291  return &t;
292 }
293 
294 QStringList NumericTextType::specialActions() const
295 {
296  QStringList ret = GenericTextType::specialActions();
297  ret << i18n( "Change &Value..." );
298  return ret;
299 }
300 
301 void NumericTextType::executeAction( int i, ObjectHolder& o, ObjectTypeCalcer& c,
302  KigPart& doc, KigWidget& w,
303  NormalMode& nm) const
304 {
305  std::vector<ObjectCalcer*> parents = c.parents();
306  assert( parents.size() == 4 );
307 
308  std::vector<ObjectCalcer*> firstthree( parents.begin(), parents.begin() + 3 );
309 
310  assert( o.imp()->inherits( NumericTextImp::stype() ) );
311  assert( argParser().checkArgs( firstthree ) );
312  assert( dynamic_cast<ObjectConstCalcer*>( firstthree[0] ) );
313  assert( dynamic_cast<ObjectConstCalcer*>( firstthree[2] ) );
314 
315  const int parentactions = GenericTextType::specialActions().count();
316  if ( i < parentactions )
317  GenericTextType::executeAction( i, o, c, doc, w, nm );
318  else if ( i == parentactions )
319  {
320  bool ok;
321  ObjectConstCalcer* valuecalcer = dynamic_cast<ObjectConstCalcer*>( parents[3] );
322  assert( valuecalcer );
323  double oldvalue = static_cast<const NumericTextImp*>( o.imp() )->getValue();
324  double value = getDoubleFromUser(
325  i18n( "Set Value" ), i18n( "Enter the new value:" ),
326  oldvalue, &w, &ok, -2147483647, 2147483647, 7 );
327  if ( ! ok ) return;
328  MonitorDataObjects mon( parents );
329  valuecalcer->setImp( new DoubleImp( value ) );
330  KigCommand* kc = new KigCommand( doc, i18n( "Change Displayed Value" ) );
331  mon.finish( kc );
332  doc.history()->push( kc );
333  }
334  else assert( false );
335 }
336 
checkArgs
static bool checkArgs(const Collection &os, uint min, const std::vector< ArgsParser::spec > &argsspec)
Definition: argsparser.cpp:199
ObjectImpType
Instances of this class represent a certain ObjectImp type.
Definition: object_imp.h:95
TextType
Definition: text_type.h:54
ObjectImp::inherits
bool inherits(const ObjectImpType *t) const
Returns true if this ObjectImp inherits the ObjectImp type represented by t.
Definition: object_imp.cc:279
ObjectConstCalcer::setImp
void setImp(ObjectImp *newimp)
Set the ObjectImp of this ObjectConstCalcer to the given newimp.
Definition: object_calcer.cc:241
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(SegmentAxisType)
point_imp.h
TextType::executeAction
void executeAction(int i, ObjectHolder &o, ObjectTypeCalcer &c, KigPart &d, KigWidget &w, NormalMode &m) const
execute the i 'th action from the specialActions above.
Definition: text_type.cc:250
StringImp
This ObjectImp is a BogusImp containing only a string value.
Definition: bogus_imp.h:167
KigCommand
a KigCommand represents almost every action performed in Kig.
Definition: kig_commands.h:44
StringImp::fillInNextEscape
void fillInNextEscape(QString &s, const KigDocument &) const
Definition: bogus_imp.cc:103
object_drawer.h
ObjectConstCalcer
This is an ObjectCalcer that keeps an ObjectImp, and never calculates a new one.
Definition: object_calcer.h:232
text_imp.h
GenericTextType
Definition: text_type.h:23
ObjectDrawer::font
QFont font() const
return the font
Definition: object_drawer.cc:149
QFont
TextImp
Definition: text_imp.h:26
KigCommand::addTask
void addTask(KigCommandTask *)
Definition: kig_commands.cpp:74
ChangeObjectConstCalcerTask
Definition: kig_commands.h:124
DoubleImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the DoubleImp type.
Definition: bogus_imp.cc:270
GenericTextType::move
void move(ObjectTypeCalcer &ourobj, const Coordinate &to, const KigDocument &) const
Definition: text_type.cc:115
KigPart::history
QUndoStack * history()
Definition: kig_part.cpp:630
ObjectHolder::drawer
const ObjectDrawer * drawer() const
Definition: object_holder.cc:58
NumericTextImp
Definition: text_imp.h:70
GenericTextType::isFreelyTranslatable
bool isFreelyTranslatable(const ObjectTypeCalcer &ourobj) const
Definition: text_type.cc:110
IntImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the IntImp type.
Definition: bogus_imp.cc:278
GenericTextType::moveReferencePoint
const Coordinate moveReferencePoint(const ObjectTypeCalcer &ourobj) const
Definition: text_type.cc:135
ObjectImp::stype
static const ObjectImpType * stype()
The ObjectImpType representing the base ObjectImp class.
Definition: object_imp.cc:284
ObjectTypeCalcer
This is an ObjectCalcer that uses one of the various ObjectType's to calculate its ObjectImp...
Definition: object_calcer.h:183
IntImp
This ObjectImp is a BogusImp containing only an int value.
Definition: bogus_imp.h:128
NumericTextType::specialActions
QStringList specialActions() const
return i18n'd names for the special actions.
Definition: text_type.cc:294
Coordinate
The Coordinate class is the basic class representing a 2D location by its x and y components...
Definition: coordinate.h:33
QClipboard
GenericTextType::movableParents
std::vector< ObjectCalcer * > movableParents(const ObjectTypeCalcer &ourobj) const
Definition: text_type.cc:159
GenericTextType::impRequirement
const ObjectImpType * impRequirement(const ObjectImp *o, const Args &parents) const
Supposing that parents would be given as parents to this type's calc function, this function returns ...
Definition: text_type.cc:66
GenericTextType::specialActions
QStringList specialActions() const
return i18n'd names for the special actions.
Definition: text_type.cc:173
QList::count
int count(const T &value) const
ChangeObjectDrawerTask
Definition: kig_commands.h:223
QApplication::clipboard
QClipboard * clipboard()
NumericTextType::executeAction
void executeAction(int i, ObjectHolder &o, ObjectTypeCalcer &c, KigPart &d, KigWidget &w, NormalMode &m) const
execute the i 'th action from the specialActions above.
Definition: text_type.cc:301
TextImp::text
QString text() const
Definition: text_imp.cc:115
getDoubleFromUser
double getDoubleFromUser(const QString &caption, const QString &label, double value, QWidget *parent, bool *ok, double min, double max, int decimals)
Here, we define some algorithms which we need in various places...
Definition: common.cpp:349
ObjectHolder
An ObjectHolder represents an object as it is known to the document.
Definition: object_holder.h:40
ObjectTypeCalcer::imp
const ObjectImp * imp() const
Returns the ObjectImp of this ObjectCalcer.
Definition: object_calcer.cc:100
bogus_imp.h
TestResultImp::stype
static const ObjectImpType * stype()
Definition: bogus_imp.cc:294
PointImp
An ObjectImp representing a point.
Definition: point_imp.h:27
BoolTextImp
Definition: text_imp.h:91
Args
std::vector< const ObjectImp * > Args
Definition: objects/common.h:47
NumericTextType
Definition: text_type.h:67
TextType::specialActions
QStringList specialActions() const
return i18n'd names for the special actions.
Definition: text_type.cc:243
MonitorDataObjects
this class monitors a set of DataObjects for changes and returns an appropriate ChangeObjectImpsComma...
Definition: kig_commands.h:153
arggspeccs
static const ArgsParser::spec arggspeccs[]
Definition: text_type.cc:43
KigPart::runMode
void runMode(KigMode *)
Definition: kig_part.cpp:732
KigWidget
This class is the real widget showing the document.
Definition: kig_view.h:50
ArgsParser::checkArgs
bool checkArgs(const std::vector< ObjectCalcer * > &os) const
Definition: argsparser.cpp:222
QString
TextLabelRedefineMode
Definition: label.h:118
GenericTextType::~GenericTextType
~GenericTextType()
Definition: text_type.cc:57
GenericTextType::sortArgs
std::vector< ObjectCalcer * > sortArgs(const std::vector< ObjectCalcer * > &os) const
Definition: text_type.cc:141
QStringList
ArgsParser::spec
Definition: argsparser.h:113
ArgsParser
This class is meant to take care of checking the types of the parents to ObjectCalcer's, and to put them in the correct order.
Definition: argsparser.h:106
MonitorDataObjects::finish
void finish(KigCommand *comm)
add the generated KigCommandTasks to the command comm .
Definition: kig_commands.cpp:227
ObjectHolder::imp
const ObjectImp * imp() const
Definition: object_holder.cc:48
ObjectType
The ObjectType class is a thing that represents the "behaviour" for a certain type.
Definition: object_type.h:32
ObjectHolder::calcer
const ObjectCalcer * calcer() const
Definition: object_holder.cc:53
GenericTextType::argParser
const ArgsParser & argParser() const
Definition: text_type.cc:130
ArgsParser::impRequirement
const ObjectImpType * impRequirement(const ObjectImp *o, const Args &parents) const
returns the minimal ObjectImp ID that o needs to inherit in order to be useful.
Definition: argsparser.cpp:185
PointImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing PointImp's.
Definition: point_imp.cc:159
line_imp.h
GenericTextType::isDefinedOnOrThrough
bool isDefinedOnOrThrough(const ObjectImp *o, const Args &parents) const
Supposing that parents would be given as parents to this type's calc function, this function returns ...
Definition: text_type.cc:168
text_type.h
GenericTextType::resultId
const ObjectImpType * resultId() const
returns the ObjectImp id of the ObjectImp's produced by this ObjectType.
Definition: text_type.cc:61
GenericTextType::canMove
bool canMove(const ObjectTypeCalcer &ourobj) const
Definition: text_type.cc:105
DoubleImp
This ObjectImp is a BogusImp containing only a double value.
Definition: bogus_imp.h:89
KigDocument
KigDocument is the class holding the real data in a Kig document.
Definition: kig_document.h:36
NumericTextImp::stype
static const ObjectImpType * stype()
Definition: text_imp.cc:189
StringImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing the StringImp type.
Definition: bogus_imp.cc:220
ObjectTypeCalcer::parents
std::vector< ObjectCalcer * > parents() const
Returns the parent ObjectCalcer's of this ObjectCalcer.
Definition: object_calcer.cc:105
GenericTextType::calc
ObjectImp * calc(const Args &parents, const KigDocument &d) const
Definition: text_type.cc:76
QClipboard::setText
void setText(const QString &text, Mode mode)
ObjectDrawer::getCopyFont
ObjectDrawer * getCopyFont(const QFont &f) const
returns a new ObjectDrawer that is identical to this one, except that the font state is set to f ...
Definition: object_drawer.cc:122
ObjectImp
The ObjectImp class represents the behaviour of an object after it is calculated. ...
Definition: object_imp.h:226
KigPart
This is a "Part".
Definition: kig_part.h:68
TextType::instance
static const TextType * instance()
Definition: text_type.cc:237
TextImp::stype
static const ObjectImpType * stype()
Definition: text_imp.cc:143
GenericTextType::GenericTextType
GenericTextType(const char *fulltypename)
Definition: text_type.cc:52
NumericTextType::instance
static const NumericTextType * instance()
Definition: text_type.cc:288
NormalMode
Definition: normal.h:26
TestResultImp
Definition: bogus_imp.h:253
QUndoStack::push
void push(QUndoCommand *cmd)
InvalidImp
This ObjectImp represents an invalid object.
Definition: bogus_imp.h:61
ArgsParser::parse
Args parse(const Args &os) const
Definition: argsparser.cpp:135
GenericTextType::executeAction
void executeAction(int i, ObjectHolder &o, ObjectTypeCalcer &c, KigPart &d, KigWidget &w, NormalMode &m) const
execute the i 'th action from the specialActions above.
Definition: text_type.cc:182
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:12:05 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
  • 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