2
This file is part of Kig, a KDE program for Interactive Geometry...
3
Copyright (C) 2012 David E. Narvaez <david.narvaez@computer.org>
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.
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.
15
You should have received a copy of the GNU General Public License along
16
with this program; if not, write to the Free Software Foundation, Inc.,
17
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
#include "builtinobjectactionsprovider.h"
23
#include "popupactionprovider.h"
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"
38
#include <KColorDialog>
43
const Qt::GlobalColor color;
47
static const color_struct colors[] =
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" ) }
58
const int numberofcolors = 7; // is there a better way to calc that?
60
void BuiltinObjectActionsProvider::fillUpMenu( NormalModePopupObjects& popup, int menu, int& nextfree )
62
KIconLoader* l = popup.part().iconLoader();
63
if ( menu == NormalModePopupObjects::ToplevelMenu )
65
std::vector<ObjectHolder*> os = popup.objects();
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.
77
if ( os.size() > 1 || os.front()->shown() )
79
popup.addInternalAction( menu, i18n( "&Hide" ), nextfree );
81
if ( os.size() == 1 && !os.front()->shown() )
83
popup.addInternalAction( menu, i18n( "&Show" ), nextfree+1 );
86
popup.addInternalAction( menu, KIcon( "transform-move", l ), i18n( "&Move" ), nextfree++ );
87
popup.addInternalAction( menu, KIcon( "edit-delete", l ), i18n( "&Delete" ), nextfree++ );
89
else if ( menu == NormalModePopupObjects::SetColorMenu )
92
for( int i = 0; i < numberofcolors; i++ )
94
p.fill( QColor( colors[i].color ) );
95
popup.addInternalAction( menu, QIcon( p ), i18n( colors[i].name ), nextfree++ );
97
popup.addInternalAction( menu, KIcon( "color", l ), i18n( "&Custom Color" ), nextfree++ );
99
else if ( menu == NormalModePopupObjects::SetSizeMenu && !popup.onlyLabels() )
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 )
107
if ( ! (*i)->imp()->inherits( PointImp::stype() ) )
109
if ( (*i)->drawer()->color() != color ) samecolor = false;
111
if ( ! samecolor ) color = Qt::blue;
113
for ( int i = 1; i < 8; ++i )
115
p.fill( popup.palette().color( popup.backgroundRole() ) );
117
ptr.setPen( QPen( color, 1 ) );
118
ptr.setBrush( QBrush( color, Qt::SolidPattern ) );
122
QRect r( ( 20 - size ) / 2, ( 20 - size ) / 2, size, size );
123
ptr.drawEllipse( r );
127
ptr.setPen( QPen( color, -1 + 2*i ) );
128
ptr.drawLine( QPoint( 0, 10 ), QPoint( 50, 10 ) );
131
popup.addInternalAction( menu, QIcon( p ), nextfree++ );
134
else if ( menu == NormalModePopupObjects::SetStyleMenu && !popup.onlyLabels() )
136
bool samecolor = true;
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 )
143
if ( (*i)->imp()->inherits( PointImp::stype() ) )
147
if ( (*i)->drawer()->color() != color ) samecolor = false;
149
bool point = ( npoints > nothers );
150
if ( ! samecolor ) color = Qt::blue;
152
for ( int i = 0; i < 5; ++i )
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++ );
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 )
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++ );
182
bool BuiltinObjectActionsProvider::executeAction(
183
int menu, int& id, const std::vector<ObjectHolder*>& os, NormalModePopupObjects& popup,
184
KigPart& doc, KigWidget& w, NormalMode& mode )
186
if ( menu == NormalModePopupObjects::ToplevelMenu )
196
// hide the objects..
197
doc.hideObjects( os );
200
// show the objects..
201
doc.showObjects( os );
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 );
211
// in this case, we return, cause we don't want objects to be
212
// unselected... ( or maybe we do ? )
217
doc.delObjects( os );
219
default: assert( false );
221
mode.clearSelection();
224
else if ( menu == NormalModePopupObjects::SetColorMenu )
226
if ( id >= numberofcolors + 1 )
228
id -= numberofcolors + 1;
232
if ( id < numberofcolors )
233
color = QColor( colors[id].color );
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;
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 );
248
else if ( menu == NormalModePopupObjects::SetSizeMenu )
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 );
262
else if ( menu == NormalModePopupObjects::SetStyleMenu )
266
for ( std::vector<ObjectHolder*>::const_iterator i = os.begin(); i != os.end(); ++i )
268
if ( (*i)->imp()->inherits( PointImp::stype() ) )
273
bool point = ( npoints > nothers );
274
int max = point ? 5 : 5;
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 );
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 );