~ubuntu-branches/ubuntu/saucy/kig/saucy

« back to all changes in this revision

Viewing changes to modes/popup/builtinobjectactionsprovider.cc

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell, Jonathan Riddell, Harald Sitter
  • Date: 2012-11-19 15:58:49 UTC
  • mfrom: (1.1.16)
  • Revision ID: package-import@ubuntu.com-20121119155849-l709sjsbjijx422a
Tags: 4:4.9.80-0ubuntu1
[ Jonathan Riddell ]
* New upstream beta release

[ Harald Sitter ]
* Add libboost-python-dev as build dep to enable python script support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
    This file is part of Kig, a KDE program for Interactive Geometry...
 
3
    Copyright (C) 2012  David E. Narvaez <david.narvaez@computer.org>
 
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 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.
 
18
*/
 
19
 
 
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