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.
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>
41 #include <kapplication.h>
42 #include <kmessagebox.h>
43 #include <kfiledialog.h>
48 using namespace KXForms
;
50 Editor::Editor( Manager
*m
)
51 : mEventLoop( new QEventLoop( this ) ), mEditMode( false ), mInEdit( false ),
52 mEditorWidget( 0 ), mManager( m
)
62 void Editor::setupActions()
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
) {
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
) {
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
) {
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())) ;
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
;
192 FormGui
*w
= mManager
->currentGui();
193 GuiElement::List list
= w
->elements();
194 mEditorWidget
= new EditorWidget( this, w
);
195 mEditorWidget
->setGuiElements( list
);
196 mEditorWidget
->show();
198 mEditorWidget
->hide();
199 mEditorWidget
->deleteLater();
203 void Editor::toggleEditMode()
205 setEditMode( !mEditMode
);
208 void Editor::actionTriggered()
210 QAction
*action
= dynamic_cast<QAction
*>( sender() );
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
];
228 void Editor::applyHint( const Hint
&h
)
231 hints
.insertHint( h
);
235 void Editor::applyHints( const Hints
&h
)
245 emit
hintsChanged( mHints
);
248 GuiElement
*Editor::selectWidget( SelectionMode sm
)
251 return mEditorWidget
->selectElement( sm
);
254 void Editor::beginEdit()
257 mEditorWidget
->setInEdit( true );
260 void Editor::finishEdit()
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() ) );
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() ) );
279 file
.write( mHints
.toXml().toLatin1() );
284 void Editor::saveHintsAs()
286 KUrl url
= KFileDialog::getSaveUrl( KUrl(), QString(), mEditorWidget
,
287 i18n("Select Hints File") );
288 if( !url
.isEmpty() && url
.isValid() ) {
292 KMessageBox::sorry( mEditorWidget
, i18n("Invalid Url '%1'.",
297 #include "editor.moc"