1 // Copyright (C) 2003 Dominique Devriese <devriese@kde.org>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA
18 #include "text_type.h"
21 #include "bogus_imp.h"
22 #include "point_imp.h"
25 #include "../kig/kig_view.h"
26 #include "../kig/kig_part.h"
27 #include "../kig/kig_commands.h"
28 #include "../modes/label.h"
29 #include "../misc/coordinate_system.h"
33 #include <qclipboard.h>
34 #include <qstringlist.h>
36 #include <kapplication.h>
40 static const ArgsParser::spec arggspeccs
[] =
42 { IntImp::stype(), "UNUSED", "SHOULD NOT BE SEEN", false },
43 { PointImp::stype(), "UNUSED", "SHOULD NOT BE SEEN", false },
44 { StringImp::stype(), "UNUSED", "SHOULD NOT BE SEEN", false }
47 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( TextType
)
50 : ObjectType( "Label" ), mparser( arggspeccs
, 3 )
58 const TextType
* TextType::instance()
60 static const TextType t
;
64 const ObjectImpType
* TextType::resultId() const
66 return TextImp::stype();
69 const ObjectImpType
* TextType::impRequirement( const ObjectImp
* o
, const Args
& args
) const
71 assert( args
.size() >= 3 );
72 Args
firstthree( args
.begin(), args
.begin() + 3 );
73 if ( o
== args
[0] || o
== args
[1] || o
== args
[2] )
74 return mparser
.impRequirement( o
, firstthree
);
76 return ObjectImp::stype();
79 ObjectImp
* TextType::calc( const Args
& parents
, const KigDocument
& doc
) const
81 if( parents
.size() < 3 ) return new InvalidImp
;
82 Args
firstthree( parents
.begin(), parents
.begin() + 3 );
83 Args
varargs( parents
.begin() + 3, parents
.end() );
85 if ( ! mparser
.checkArgs( firstthree
) ) return new InvalidImp
;
87 int frame
= static_cast<const IntImp
*>( firstthree
[0] )->data();
88 bool needframe
= frame
!= 0;
89 const Coordinate t
= static_cast<const PointImp
*>( firstthree
[1] )->coordinate();
90 QString s
= static_cast<const StringImp
*>( firstthree
[2] )->data();
92 for ( Args::iterator i
= varargs
.begin(); i
!= varargs
.end(); ++i
)
93 (*i
)->fillInNextEscape( s
, doc
);
95 return new TextImp( s
, t
, needframe
);
98 bool TextType::canMove( const ObjectTypeCalcer
& ) const
103 bool TextType::isFreelyTranslatable( const ObjectTypeCalcer
& ) const
108 void TextType::move( ObjectTypeCalcer
& ourobj
, const Coordinate
& to
,
109 const KigDocument
& d
) const
111 const std::vector
<ObjectCalcer
*> parents
= ourobj
.parents();
112 assert( parents
.size() >= 3 );
113 const std::vector
<ObjectCalcer
*> firstthree( parents
.begin(), parents
.begin() + 3 );
114 if( dynamic_cast<ObjectConstCalcer
*>( firstthree
[1] ) )
116 ObjectConstCalcer
* c
= static_cast<ObjectConstCalcer
*>( firstthree
[1] );
117 c
->setImp( new PointImp( to
) );
120 firstthree
[1]->move( to
, d
);
123 QStringList
TextType::specialActions() const
126 ret
<< i18n( "&Copy Text" );
127 ret
<< i18n( "&Toggle Frame" );
128 ret
<< i18n( "&Redefine..." );
132 void TextType::executeAction( int i
, ObjectHolder
& o
, ObjectTypeCalcer
& c
,
133 KigPart
& doc
, KigWidget
&,
136 std::vector
<ObjectCalcer
*> parents
= c
.parents();
137 assert( parents
.size() >= 3 );
139 std::vector
<ObjectCalcer
*> firstthree( parents
.begin(), parents
.begin() + 3 );
141 assert( mparser
.checkArgs( firstthree
) );
142 assert( dynamic_cast<ObjectConstCalcer
*>( firstthree
[0] ) );
143 assert( dynamic_cast<ObjectConstCalcer
*>( firstthree
[2] ) );
147 QClipboard
* cb
= kapp
->clipboard();
149 // copy the text into the clipboard
150 const TextImp
* ti
= static_cast<const TextImp
*>( c
.imp() );
151 cb
->setText( ti
->text(), QClipboard::Clipboard
);
155 // toggle label frame
156 int n
= (static_cast<const IntImp
*>( firstthree
[0]->imp() )->data() + 1) % 2;
157 KigCommand
* kc
= new KigCommand( doc
, i18n( "Toggle Label Frame" ) );
158 kc
->addTask( new ChangeObjectConstCalcerTask(
159 static_cast<ObjectConstCalcer
*>( firstthree
[0] ),
161 doc
.history()->addCommand( kc
);
165 assert( dynamic_cast<ObjectTypeCalcer
*>( o
.calcer() ) );
167 TextLabelRedefineMode
m( doc
, static_cast<ObjectTypeCalcer
*>( o
.calcer() ) );
170 else assert( false );
173 const ArgsParser
& TextType::argParser() const
178 const Coordinate
TextType::moveReferencePoint( const ObjectTypeCalcer
& ourobj
) const
180 assert( ourobj
.imp()->inherits( TextImp::stype() ) );
181 return static_cast<const TextImp
*>( ourobj
.imp() )->coordinate();
184 std::vector
<ObjectCalcer
*> TextType::sortArgs( const std::vector
<ObjectCalcer
*>& os
) const
186 assert( os
.size() >= 3 );
187 std::vector
<ObjectCalcer
*> ret( os
.begin(), os
.begin() + 3 );
188 ret
= mparser
.parse( ret
);
189 std::copy( os
.begin() + 3, os
.end(), std::back_inserter( ret
) );
193 Args
TextType::sortArgs( const Args
& args
) const
195 assert( args
.size() >= 3 );
196 Args
ret( args
.begin(), args
.begin() + 3 );
197 ret
= mparser
.parse( ret
);
198 std::copy( args
.begin() + 3, args
.end(), std::back_inserter( ret
) );
202 std::vector
<ObjectCalcer
*> TextType::movableParents( const ObjectTypeCalcer
& ourobj
) const
204 const std::vector
<ObjectCalcer
*> parents
= ourobj
.parents();
205 assert( parents
.size() >= 3 );
206 std::vector
<ObjectCalcer
*> ret
= parents
[1]->movableParents();
207 ret
.push_back( parents
[1] );
211 bool TextType::isDefinedOnOrThrough( const ObjectImp
*, const Args
& ) const