popup.cc 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
 This file is part of Kig, a KDE program for Interactive Geometry...
 Copyright (C) 2002  Dominique Devriese <devriese@kde.org>

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 USA
**/

#include "popup.h"

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include "../../kig/kig_part.h"
#include "../../kig/kig_document.h"
#include "../../kig/kig_view.h"
#include "../../kig/kig_commands.h"
#include "../../objects/object_imp.h"
#include "../../objects/object_drawer.h"
#include "../../objects/bogus_imp.h"
#include "../../objects/point_imp.h"
#include "../../objects/line_imp.h"
#include "../../objects/other_type.h"
#include "../../objects/object_factory.h"
#include "../../objects/polygon_imp.h"
#include "../../objects/text_imp.h"
#include "../../objects/text_type.h"
#include "../../misc/lists.h"
#include "../../misc/argsparser.h"
#include "../../misc/kigpainter.h"
#include "../../misc/coordinate_system.h"
#include "../../misc/object_constructor.h"
#include "../construct_mode.h"
#include "../normal.h"
#include "../moving.h"

#include "builtindocumentactionsprovider.h"
#include "builtinobjectactionsprovider.h"
#include "nameobjectactionsprovider.h"
#include "objectconstructoractionsprovider.h"
#include "objecttypeactionsprovider.h"
#include "propertiesactionsprovider.h"
52 53 54 55

#include <algorithm>
#include <functional>

David Narváez's avatar
David Narváez committed
56
#include <QIcon>
57 58 59 60 61 62 63 64
#include <qaction.h>
#include <qcursor.h>
#include <qdialog.h>
#include <qfont.h>
#include <qpen.h>
#include <qregexp.h>
#include <qvalidator.h>

David Narváez's avatar
David Narváez committed
65
#include <QAction>
66 67

#include <KIconEngine>
68 69 70 71 72 73
#include <kcolordialog.h>
#include <kinputdialog.h>
#include <klocale.h>

#include <config-kig.h>

74 75 76 77
#ifdef KIG_ENABLE_PYTHON_SCRIPTING
#include "scriptactionsprovider.h"
#endif

78 79 80 81 82 83 84 85 86 87 88 89 90
#define MAXMENUITEMS 20
/*
 * mp: if a submenus requires more than MAXMENUITEMS items then a new
 * submenu titled " more..." is added
 */

using namespace std;

NormalModePopupObjects::NormalModePopupObjects( KigPart& part,
                                                KigWidget& view,
                                                NormalMode& mode,
                                                const std::vector<ObjectHolder*>& objs,
                                                const QPoint& plc )
David Narváez's avatar
David Narváez committed
91
  : QMenu( &view ), mplc( plc ), mpart( part ), mview( view ), mobjs( objs ),
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    mmode( mode ), monlylabels( false )
{
  bool empty = objs.empty();
  bool single = objs.size() == 1;

  QString title;
  if ( empty )
    title = i18n( "Kig Document" );
  else if ( single )
  {
    if ( !objs[0]->name().isNull() )
      title = QString::fromLatin1( "%1 %2" ).arg( objs[0]->imp()->type()->translatedName() ).arg( objs[0]->name() );
    else
      title = objs[0]->imp()->type()->translatedName();
  }
  else
    title = i18np( "%1 Object", "%1 Objects", objs.size() );
David Narváez's avatar
David Narváez committed
109
  addSection( title );
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

  if ( !empty )
  {
    monlylabels = true;
    uint i = 0;
    while ( i < objs.size() && monlylabels )
    {
      monlylabels &= objs[i]->imp()->inherits( TextImp::stype() );
      ++i;
    }
  }

  if ( empty )
  {
    // provides some diverse stuff like "unhide all", set coordinate
    // system etc.
    mproviders.push_back( new BuiltinDocumentActionsProvider() );
  };
  // construct an object using these objects and start constructing an
  // object using these objects
  mproviders.push_back( new ObjectConstructorActionsProvider() );
  if ( single )
    mproviders.push_back( new NameObjectActionsProvider() );
  if ( ! empty )
  {
    // stuff like hide, show, delete, set size, set color..
    mproviders.push_back( new BuiltinObjectActionsProvider() );
    // show property as text label -> show menu
    // and construct property's as objects -> construct menu
    mproviders.push_back( new PropertiesActionsProvider() );
    // stuff like "redefine point" for a fixed or constrained point..
    mproviders.push_back( new ObjectTypeActionsProvider() );
  }
#ifdef KIG_ENABLE_PYTHON_SCRIPTING
  // script action..
  mproviders.push_back( new ScriptActionsProvider() );
#endif

  static const QString menunames[NumberOfMenus] =
    {
      i18n( "&Transform" ),
      i18n( "T&est" ),
      i18n( "Const&ruct" ),
      i18n( "&Start" ),
      i18n( "Add Te&xt Label" ),
      i18n( "Set Co&lor" ),
      i18n( "Set &Pen Width" ),
      i18n( "Set St&yle" ),
      QString(),
      i18n( "Set Coordinate S&ystem" )
    };
  static const QString menuicons[NumberOfMenus] =
    {
      "centralsymmetry",
      "test",
      QString(),
      "system-run",
      "kig_text", // <- draw-text... hope I manage to rename it in time
      "format-fill-color",
//      "colorize",
      "sizer",
      "draw-brush",
      QString(),
      QString()
    };

  // creating the menus and setting their title and icon
  KIconLoader* l = part.iconLoader();
  for ( uint i = 0; i < NumberOfMenus; ++i )
  {
    if ( i == ToplevelMenu ) continue;
    mmenus[i] = mmenuslast[i] = new QMenu( this );
    if ( !menunames[i].isEmpty() )
      mmenus[i]->setTitle( menunames[i] );
    if ( !menuicons[i].isEmpty() )
    {
186
      mmenus[i]->setIcon( QIcon( new KIconEngine( menuicons[i], l ) ) );
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    }
  }
  mmenus[ToplevelMenu] = mmenuslast[ToplevelMenu] = this;
  /*
   * mp: previously ToplevelMenu was treated like a submenu (of itself).
   * unfortunately this had as side effect a duplicated triggering of its
   * actions with unpredictable consequences.  In this way the addAction
   * in addInternalAction directly adds the action at toplevel.
   */

  connect( this, SIGNAL( triggered( QAction* ) ),
           this, SLOT( toplevelMenuSlot( QAction* ) ) );

  for ( int i = 0; i < NumberOfMenus; ++i )
  {
    int nextfree = 10;
    for ( uint j = 0; j < mproviders.size(); ++j )
      mproviders[j]->fillUpMenu( *this, i, nextfree );
  };
  QAction* firstrealaction = actions()[1];
  for ( int i = 0; i < NumberOfMenus; ++i )
  {
    if ( i == ToplevelMenu ) continue;
    if ( mmenus[i]->actions().count() == 0 ) continue;
    //addMenu( mmenus[i] );
    insertMenu( firstrealaction, mmenus[i] );
  };
  // addActions( mmenus[ToplevelMenu]->actions() );
}

void NormalModePopupObjects::toplevelMenuSlot( QAction* act )
{
//  activateAction( ToplevelMenu, i );
  int data = act->data().toInt();
  int id = data & 0xFF;
  int menu = data >> 8;
David Narváez's avatar
David Narváez committed
223
qDebug() << "menu: " << menu << " - id: " << id;
224 225 226 227 228 229 230 231 232
  activateAction( menu, id );
}

void NormalModePopupObjects::activateAction( int menu, int action )
{
  bool done = false;
  // we need action - 10 cause we called fillUpMenu with nextfree set
  // to 10 initially..
  action -= 10;
David Narváez's avatar
David Narváez committed
233
qDebug() << "MENU: " << menu << " - ACTION: " << action;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  for ( uint i = 0; ! done && i < mproviders.size(); ++i )
    done = mproviders[i]->executeAction( menu, action, mobjs, *this, mpart, mview, mmode );
}

NormalModePopupObjects::~NormalModePopupObjects()
{
  delete_all ( mproviders.begin(), mproviders.end() );
}

QAction* NormalModePopupObjects::addInternalAction( int menu, const QIcon& pix, int id )
{
  return addInternalAction( menu, pix, "", id );
}

QAction* NormalModePopupObjects::addInternalAction( int menu, const QIcon& icon, const QString& name, int id )
{
  if ( mmenuslast[menu]->actions().size() >= MAXMENUITEMS )
    mmenuslast[menu] = mmenuslast[menu]->addMenu( i18nc( "More menu items", "More..." ) );
  QAction* newaction = mmenuslast[menu]->addAction( icon, name );
  newaction->setData( QVariant::fromValue( ( menu << 8 ) | id ) );
  return newaction;
}

QAction* NormalModePopupObjects::addInternalAction( int menu, const QString& name, int id )
{
  if ( mmenuslast[menu]->actions().size() >= MAXMENUITEMS )
    mmenuslast[menu] = mmenuslast[menu]->addMenu( i18nc( "More menu items", "More..." ) );
  QAction* newaction = mmenuslast[menu]->addAction( name );
  newaction->setData( QVariant::fromValue( ( menu << 8 ) | id ) );
  return newaction;
}

QAction* NormalModePopupObjects::addInternalAction( int menu, QAction* act )
{
  if ( mmenuslast[menu]->actions().size() >= MAXMENUITEMS )
    mmenuslast[menu] = mmenuslast[menu]->addMenu( i18nc( "More menu items", "More..." ) );
  mmenuslast[menu]->addAction( act );
  return act;
}