foreach and qDeleteAll know how to iterate over qmaps, so do not use values, it's...
[kdepim.git] / kode / kxforms / editor / editor.cpp
blob77c6baee9114263be71ab95e392c4fe34cb4fa48
1 /*
2 This file is part of KXForms.
4 Copyright (c) 2007 Andre Duffeck <aduffeck@suse.de>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 #include "editor.h"
23 #include "editorwidget.h"
24 #include "editoraction.h"
25 #include "changelabelaction.h"
26 #include "appearanceaction.h"
27 #include "listaction.h"
28 #include "positionaction.h"
29 #include "layoutstyleaction.h"
30 #include "groupaction.h"
31 #include "readonlyaction.h"
32 #include "inputtypeaction.h"
34 #include "../manager.h"
35 #include "../guielement.h"
37 #include <kactionmenu.h>
38 #include <kdebug.h>
39 #include <klocale.h>
40 #include <kmenu.h>
41 #include <kapplication.h>
42 #include <kmessagebox.h>
43 #include <kfiledialog.h>
45 #include <QEvent>
46 #include <QEventLoop>
48 using namespace KXForms;
50 Editor::Editor( Manager *m)
51 : mEventLoop( new QEventLoop( this ) ), mEditMode( false ), mInEdit( false ),
52 mEditorWidget( 0 ), mManager( m )
54 setupActions();
57 Editor::~Editor()
59 qDeleteAll(mActions);
62 void Editor::setupActions()
64 EditorAction *a;
66 a = new ChangeLabelAction( this );
67 connect( a, SIGNAL(hintGenerated( const Hint & )),
68 SLOT(applyHint( const Hint & )) );
69 mActions[ "edit_label" ] = a;
71 a = new AppearanceAction( this );
72 connect( a, SIGNAL(hintGenerated( const Hint & )),
73 SLOT(applyHint( const Hint & )) );
74 mActions[ "edit_appearance" ] = a;
76 a = new ListAction( this );
77 connect( a, SIGNAL(hintGenerated( const Hint & )),
78 SLOT(applyHint( const Hint & )) );
79 mActions[ "edit_list" ] = a;
81 a = new PositionAction( this );
82 connect( a, SIGNAL(hintGenerated( const Hint & )),
83 SLOT(applyHint( const Hint & )) );
84 mActions[ "edit_position" ] = a;
86 a = new LayoutStyleAction( this );
87 connect( a, SIGNAL(hintGenerated( const Hint & )),
88 SLOT(applyHint( const Hint & )) );
89 mActions[ "edit_layoutstyle" ] = a;
91 a = new GroupAction( this );
92 connect( a, SIGNAL(hintGenerated( const Hint & )),
93 SLOT(applyHint( const Hint & )) );
94 mActions[ "edit_group" ] = a;
96 a = new ReadOnlyAction( this );
97 connect( a, SIGNAL(hintGenerated( const Hint & )),
98 SLOT(applyHint( const Hint & )) );
99 mActions[ "edit_readonly" ] = a;
101 a = new InputTypeAction( this );
102 connect( a, SIGNAL(hintGenerated( const Hint & )),
103 SLOT(applyHint( const Hint & )) );
104 mActions[ "edit_inputtype" ] = a;
107 KActionMenu *Editor::actionMenu( GuiElement *e )
109 KActionMenu *menu = new KActionMenu( this );
110 bool needSeparator = false;
111 if( e->actionTypes() & Editor::CommonActions ) {
112 needSeparator = true;
113 KAction *titleAction = new KAction( i18n("Change Label"), menu );
114 titleAction->setData( "edit_label" );
115 QObject::connect( titleAction, SIGNAL(triggered(bool)), SLOT( actionTriggered() ) );
116 menu->addAction( titleAction );
119 KAction *layoutStyleAction = new KAction( i18n("Change Layout Style"), menu );
120 layoutStyleAction->setData( "edit_layoutstyle" );
121 QObject::connect( layoutStyleAction, SIGNAL(triggered(bool)), SLOT( actionTriggered() ) );
122 menu->addAction( layoutStyleAction );
124 KAction *readonlyAction = new KAction( i18n("Change ReadOnly Mode"), menu );
125 readonlyAction->setData( "edit_readonly" );
126 QObject::connect( readonlyAction, SIGNAL(triggered(bool)), SLOT( actionTriggered() ) );
127 menu->addAction( readonlyAction );
130 KAction *positionAction = new KAction( i18n("Change Position"), menu );
131 positionAction->setData( "edit_position" );
132 QObject::connect( positionAction, SIGNAL(triggered(bool)), SLOT( actionTriggered() ) );
133 menu->addAction( positionAction );
136 KAction *groupAction = new KAction( i18n("Change Group"), menu );
137 groupAction->setData( "edit_group" );
138 QObject::connect( groupAction, SIGNAL(triggered(bool)), SLOT( actionTriggered() ) );
139 menu->addAction( groupAction );
142 if( e->actionTypes() & Editor::AppearanceActions ) {
143 if( needSeparator )
144 menu->addSeparator();
145 needSeparator = true;
147 KAction *appearanceAction = new KAction( i18n("Change Appearance"), menu );
148 appearanceAction->setData( "edit_appearance" );
149 QObject::connect( appearanceAction, SIGNAL(triggered(bool)), SLOT( actionTriggered() ) );
150 menu->addAction( appearanceAction );
153 if( e->actionTypes() & Editor::ListActions ) {
154 if( needSeparator )
155 menu->addSeparator();
156 needSeparator = true;
158 KAction *listAction = new KAction( i18n("Change List Properties"), menu );
159 listAction->setData( "edit_list" );
160 QObject::connect( listAction, SIGNAL(triggered(bool)), SLOT( actionTriggered() ) );
161 menu->addAction( listAction );
164 if( e->actionTypes() & Editor::InputActions ) {
165 if( needSeparator )
166 menu->addSeparator();
167 needSeparator = true;
169 KAction *typeAction = new KAction( i18n("Change Input Type"), menu );
170 typeAction->setData( "edit_inputtype" );
171 QObject::connect( typeAction, SIGNAL(triggered(bool)), SLOT( actionTriggered() ) );
172 menu->addAction( typeAction );
175 connect( menu->menu(), SIGNAL(aboutToHide()), menu, SLOT(deleteLater())) ;
176 return menu;
179 void Editor::registerElement( GuiElement *element )
181 kDebug() <<"Registered element" << element->ref().toString();
182 mElements.append( element );
185 void Editor::setEditMode( bool enabled )
187 kDebug() <<"Setting editmode to" << enabled;
188 mEditMode = enabled;
191 if( enabled ) {
192 FormGui *w = mManager->currentGui();
193 GuiElement::List list = w->elements();
194 mEditorWidget = new EditorWidget( this, w );
195 mEditorWidget->setGuiElements( list );
196 mEditorWidget->show();
197 } else {
198 mEditorWidget->hide();
199 mEditorWidget->deleteLater();
203 void Editor::toggleEditMode()
205 setEditMode( !mEditMode );
208 void Editor::actionTriggered()
210 QAction *action = dynamic_cast<QAction *>( sender() );
211 if( !action )
212 return;
214 performAction( action->data().toString(), mEditorWidget->hoveredElement() );
217 void Editor::performAction( const QString &actionId, GuiElement *e )
219 kDebug() <<"Performing action" << actionId;
221 EditorAction *a = mActions[ actionId ];
222 if( !a )
223 return;
225 a->perform( e );
228 void Editor::applyHint( const Hint &h )
230 Hints hints;
231 hints.insertHint( h );
232 applyHints( hints );
235 void Editor::applyHints( const Hints &h )
237 kDebug() ;
239 mHints.dump();
240 mHints.merge( h );
241 mHints.dump();
243 mElements.clear();
244 mEditMode = false;
245 emit hintsChanged( mHints );
248 GuiElement *Editor::selectWidget( SelectionMode sm )
250 kDebug() ;
251 return mEditorWidget->selectElement( sm );
254 void Editor::beginEdit()
256 mInEdit = true;
257 mEditorWidget->setInEdit( true );
260 void Editor::finishEdit()
262 mInEdit = false;
263 mEditorWidget->setInEdit( false );
266 void Editor::saveHints()
268 if( mHintsUrl.isEmpty() || !mHintsUrl.isValid() ) {
269 KMessageBox::sorry( mEditorWidget, i18n("Invalid Url '%1'.",
270 mHintsUrl.prettyUrl() ) );
271 return;
272 } else {
273 QFile file( mHintsUrl.path() );
274 if( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) ) {
275 KMessageBox::sorry( mEditorWidget, i18n("Can not open '%1'.",
276 mHintsUrl.prettyUrl() ) );
277 return;
279 file.write( mHints.toXml().toLatin1() );
280 file.close();
284 void Editor::saveHintsAs()
286 KUrl url = KFileDialog::getSaveUrl( KUrl(), QString(), mEditorWidget,
287 i18n("Select Hints File") );
288 if( !url.isEmpty() && url.isValid() ) {
289 mHintsUrl = url;
290 saveHints();
291 } else {
292 KMessageBox::sorry( mEditorWidget, i18n("Invalid Url '%1'.",
293 url.prettyUrl() ) );
297 #include "editor.moc"