OpenASIP 2.2
Loading...
Searching...
No Matches
GUIOptionsSerializer.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file GUIOptionsSerializer.cc
26 *
27 * Implementation of GUIOptionsSerializer class.
28 *
29 * @author Lasse Laasonen 2004 (lasse.laasonen-no.spam-tut.fi)
30 * @note rating: red
31 */
32
33#include "Application.hh"
35#include "KeyboardShortcut.hh"
36#include "Conversion.hh"
37#include "GUIOptions.hh"
38#include "ObjectState.hh"
39
40using std::string;
41
42/// Name of the element containing window declaration.
43const string WINDOW = "window";
44/// Name of the full-screen element inside window element.
45const string WI_FULLSCREEN = "full-screen";
46/// Name of the element declaring window width.
47const string WI_WIDTH = "width";
48/// Name of the element declaring window height.
49const string WI_HEIGHT = "height";
50/// Name of the element declaring x position of the window.
51const string WI_X_POSITION = "x-position";
52/// Name of the element declaring y position of the window.
53const string WI_Y_POSITION = "y-position";
54/// Name of the element containing toolbar declaration.
55const string TOOLBAR = "toolbar";
56/// Name of the element containing toolbar layout declaration.
57const string TB_LAYOUT = "layout";
58/// Name of the text element inside layout element.
59const string TB_LA_TEXT = "text";
60/// Name of the icon element insie layout element.
61const string TB_LA_ICON = "icon";
62/// Name of the element declaring toolbar visibility.
63const string TB_VISIBLE = "visible";
64/// Value used for true in attribute and element values.
65const string TRUE = "true";
66/// Value used for false in attribute and element values.
67const string FALSE = "false";
68/// Name of the element declaring a toolbar button.
69const string TB_SLOT = "slot";
70/// Name of the attribute of the slot element.
71const string SLOT_POSITION = "position";
72/// Name of the element declaring action name inside slot element.
73const string TB_SLOT_ACTION = "action";
74/// Name of the element declaring a toolbar separator.
75const string TB_SEPARATOR = "separator";
76/// Name of the attribute of the separator element.
77const string SEPARATOR_POSITION = "position";
78/// Name of the element declaring a keyboard shortcut.
79const string KEYBOARD_SHORTCUT = "keyboard-shortcut";
80/// Name of the element declaring key combination.
81const string KS_KEY_COMBINATION = "key-combination";
82/// Name of the element meaning ctrl key in key combination.
83const string KEY_COMB_CTRL = "ctrl";
84/// Name of the element meaning alt key in key combination.
85const string KEY_COMB_ALT = "alt";
86/// Name of the element meaning some function key in key combination.
87const string KEY_COMB_FKEY = "F-key";
88/// Name of the attribute declaring function key number.
89const string FKEY_NUMBER = "number";
90/// Name of the element meaning some other key in key combination.
91const string KEY_COMB_KEY = "key";
92/// Name of the attribute which declares the actual key of the key element.
93const string KEY_VALUE = "value";
94/// Name of the element declaring the action performed by the keyboard shortcut
95const string KS_ACTION = "action";
96
97/// Default window width.
98const int DEFAULT_WIDTH = 600;
99/// Default window height.
100const int DEFAULT_HEIGHT = 500;
101/// Default x position.
102const int DEFAULT_X_POS = 0;
103/// Default y position.
104const int DEFAULT_Y_POS = 0;
105/// String to mean delete key.
106const string DELETE_KEY = "del";
107
108/**
109 * Constructor.
110 *
111 * @param name Name of the configuration.
112 */
115 configurationName_(name) {
116}
117
118
119/**
120 * Destructor.
121 */
124
125
126/**
127 * Serializes the given ObjectState tree created by GUIOptions::saveState
128 * to an XML file.
129 *
130 * @param optionsState ObjectState tree created by GUIOptions::saveState.
131 * @exception SerializerException If the current destination file cannot be
132 * written.
133 */
134void
136 ObjectState* converted = convertToConfigFileFormat(optionsState);
137 XMLSerializer::writeState(converted);
138 delete converted;
139}
140
141/**
142 * Reads the options from the current XML file set and creates an
143 * ObjectState tree which can be read by GUIOptions::loadState.
144 *
145 * @return The newly created ObjectState tree.
146 * @exception SerializerException If an error occurs while reading the file.
147 */
151 ObjectState* converted = convertToOptionsObjectFormat(state);
152 delete state;
153 return converted;
154}
155
156/**
157 * Serializes the given options to the file set.
158 *
159 * @param options The options to be serialized.
160 * @exception SerializerException If an error occurs while serializing.
161 */
162void
164 ObjectState* optionsState = options.saveState();
165 writeState(optionsState);
166 delete optionsState;
167}
168
169/**
170 * Reads the current input file and creates GUIOptions according to it.
171 *
172 * @return The newly created GUIOptions instance.
173 * @exception SerializerException If an error occurs while reading the file.
174 * @exception ObjectStateLoadingException If an error occurs while creating
175 * options.
176 */
179 ObjectState* optionsState = readState();
180 GUIOptions* options = new GUIOptions(optionsState);
181 delete optionsState;
182 return options;
183}
184
185/**
186 * Converts the given ObjectState tree created by GUIOptions::saveState
187 * to the format of configuration file.
188 *
189 * @param options ObjectState tree to be converted.
190 * @return The newly created ObjectState tree which matches with
191 * configuration file format.
192 */
195 const ObjectState* options) const {
196
198
199 // add window element
200 ObjectState* window = new ObjectState(WINDOW);
201 root->addChild(window);
202
203 ObjectState* fullScreen = new ObjectState(WI_FULLSCREEN);
204 window->addChild(fullScreen);
205 if (options->intAttribute(GUIOptions::OSKEY_FULL_SCREEN)) {
206 fullScreen->setValue(TRUE);
207 } else {
208 fullScreen->setValue(FALSE);
209 }
210
211 ObjectState* width = new ObjectState(WI_WIDTH);
212 window->addChild(width);
213 width->setValue(options->stringAttribute(GUIOptions::OSKEY_WINDOW_WIDTH));
214
215 ObjectState* height = new ObjectState(WI_HEIGHT);
216 window->addChild(height);
217 height->setValue(
218 options->stringAttribute(GUIOptions::OSKEY_WINDOW_HEIGHT));
219
221 window->addChild(xPos);
222 xPos->setValue(options->stringAttribute(GUIOptions::OSKEY_X_POS));
223
225 window->addChild(yPos);
226 yPos->setValue(options->stringAttribute(GUIOptions::OSKEY_Y_POS));
227
228 // add toolbar element
229 ObjectState* toolbar = new ObjectState(TOOLBAR);
230 root->addChild(toolbar);
231
232 ObjectState* layout = new ObjectState(TB_LAYOUT);
233 toolbar->addChild(layout);
234
235 string toolbarLayout = options->
236 stringAttribute(GUIOptions::OSKEY_TOOLBAR_LAYOUT);
237 if (toolbarLayout == GUIOptions::OSVALUE_TEXT) {
238 layout->addChild(new ObjectState(TB_LA_TEXT));
239 } else if (toolbarLayout == GUIOptions::OSVALUE_ICON) {
240 layout->addChild(new ObjectState(TB_LA_ICON));
241 } else {
242 layout->addChild(new ObjectState(TB_LA_TEXT));
243 layout->addChild(new ObjectState(TB_LA_ICON));
244 }
245
246 ObjectState* visible = new ObjectState(TB_VISIBLE);
247 toolbar->addChild(visible);
248 int vis =
250 if (vis) {
251 visible->setValue(TRUE);
252 } else {
253 visible->setValue(FALSE);
254 }
255
256 // add toolbar buttons
257 for (int i = 0; i < options->childCount(); i++) {
258 ObjectState* child = options->child(i);
260 ObjectState* slot = new ObjectState(TB_SLOT);
261 toolbar->addChild(slot);
263 child->stringAttribute(
266 slot->addChild(action);
267 action->setValue(child->stringAttribute(
269 }
270 }
271
272 // add toolbar separators
273 for (int i = 0; i < options->childCount(); i++) {
274 ObjectState* child = options->child(i);
275 if (child->name() == GUIOptions::OSNAME_SEPARATOR) {
276 ObjectState* separator = new ObjectState(TB_SEPARATOR);
277 toolbar->addChild(separator);
279 child->stringAttribute(
281 }
282 }
283
284 // add keyboard shortcuts
285 for (int i = 0; i < options->childCount(); i++) {
286 ObjectState* child = options->child(i);
287 if (child->name() !=
289 continue;
290 }
292 root->addChild(sc);
294 sc->addChild(keyComb);
295
297 keyComb->addChild(new ObjectState(KEY_COMB_CTRL));
298 }
300 keyComb->addChild(new ObjectState(KEY_COMB_ALT));
301 }
304 keyComb->addChild(fKey);
306 child->stringAttribute(
308 }
311 keyComb->addChild(key);
312 char charKey = child->intAttribute(
314 string stringKey;
315
316 // check if key is 'del'
317 if (charKey == 127) {
318 stringKey = DELETE_KEY;
319 } else {
320 stringKey = Conversion::toString(charKey);
321 }
322
323 key->setAttribute(KEY_VALUE, stringKey);
324 }
325 ObjectState* action = new ObjectState(KS_ACTION);
326 sc->addChild(action);
327 action->setValue(child->stringAttribute(
329 }
330
331 return root;
332}
333
334
335/**
336 * Creates a new ObjectState tree which can be given to GUIOptions
337 * constructor. The tree is created according to the given tree which matches
338 * with the syntax of the options file.
339 *
340 * @param root Root node of the ObjectState tree to be converted.
341 */
344 const ObjectState* root) const {
345
347
348 // set window properties
349 if (root->hasChild(WINDOW)) {
350 ObjectState* window = root->childByName(WINDOW);
352 } else {
353 options->setAttribute(GUIOptions::OSKEY_FULL_SCREEN, true);
358 }
359
360 // set toolbar properties
361 if (root->hasChild(TOOLBAR)) {
362 ObjectState* toolbar = root->childByName(TOOLBAR);
364 } else {
365 options->setAttribute(GUIOptions::OSKEY_TOOLBAR_VISIBILITY, false);
368 }
369
370 // set keyboard shortcuts
371 for (int i = 0; i < root->childCount(); i++) {
372 ObjectState* child = root->child(i);
373 if (child->name() == KEYBOARD_SHORTCUT) {
375 }
376 }
377
378 return options;
379}
380
381
382/**
383 * Sets the window properties for the given options according to the given
384 * window element.
385 *
386 * @param windowElem ObjectState representing window element in options file.
387 * @param options Options ObjectState which is modified.
388 */
389void
391 const ObjectState* windowElem,
392 ObjectState* options) const {
393
394 ObjectState* fullscreenElem = windowElem->childByName(WI_FULLSCREEN);
395 if (fullscreenElem->stringValue() == TRUE) {
396 options->setAttribute(GUIOptions::OSKEY_FULL_SCREEN, true);
397 } else if (fullscreenElem->stringValue() == FALSE) {
398 options->setAttribute(GUIOptions::OSKEY_FULL_SCREEN, false);
399 } else {
400 assert(false);
401 }
402
403 ObjectState* widthElem = windowElem->childByName(WI_WIDTH);
405 widthElem->stringValue());
406
407 ObjectState* heightElem = windowElem->childByName(WI_HEIGHT);
409 heightElem->stringValue());
410
411 ObjectState* xPosElem = windowElem->childByName(WI_X_POSITION);
412 options->setAttribute(GUIOptions::OSKEY_X_POS,
413 xPosElem->stringValue());
414
415 ObjectState* yPosElem = windowElem->childByName(WI_Y_POSITION);
416 options->setAttribute(GUIOptions::OSKEY_Y_POS,
417 yPosElem->stringValue());
418}
419
420
421/**
422 * Sets the toolbar properties for the given options according to the given
423 * toolbar element.
424 *
425 * @param toolbarElem ObjectState representing toolbar element in options
426 * file.
427 * @param options Options ObjectState which is modified.
428 */
429void
431 const ObjectState* toolbarElem,
432 ObjectState* options) const {
433
434 // set layout
435 ObjectState* layoutElem = toolbarElem->childByName(TB_LAYOUT);
436 if (layoutElem->hasChild(TB_LA_TEXT) &&
437 layoutElem->hasChild(TB_LA_ICON)) {
440 } else if (layoutElem->hasChild(TB_LA_TEXT)) {
443 } else if (layoutElem->hasChild(TB_LA_ICON)) {
446 } else {
447 assert(false);
448 }
449
450 // set visibility
451 ObjectState* visibleElem = toolbarElem->childByName(TB_VISIBLE);
452 if (visibleElem->stringValue() == TRUE) {
454 true);
455 } else if (visibleElem->stringValue() == FALSE) {
457 false);
458 }
459
460 // set buttons and separators
461 for (int i = 0; i < toolbarElem->childCount(); i++) {
462 ObjectState* child = toolbarElem->child(i);
463 if (child->name() == TB_SLOT) {
464 ObjectState* button = new ObjectState(
466 options->addChild(button);
467
468 string slot = child->stringAttribute(SLOT_POSITION);
470
471 ObjectState* action = child->childByName(TB_SLOT_ACTION);
472 string actionString = action->stringValue();
473 button->setAttribute(ToolbarButton::OSKEY_ACTION, actionString);
474
475 } else if (child->name() == TB_SEPARATOR) {
476 ObjectState* separator =
478 options->addChild(separator);
479 string position = child->stringAttribute(SEPARATOR_POSITION);
480 separator->setAttribute(GUIOptions::OSKEY_POSITION, position);
481 }
482 }
483}
484
485
486/**
487 * Adds a keyboard shortcut to the given options according to the given
488 * keyboard-shortcut element.
489 *
490 * @param ksElem ObjectState representing a keyboard-shortcut element in
491 * options file.
492 * @param options Options ObjectState which is modified.
493 */
494void
496 const ObjectState* ksElem,
497 ObjectState* options) const {
498
499 ObjectState* shortcut =
501 options->addChild(shortcut);
502
503 ObjectState* keyCombElem = ksElem->childByName(KS_KEY_COMBINATION);
504 if (keyCombElem->hasChild(KEY_COMB_CTRL)) {
506 } else {
508 }
509
510 if (keyCombElem->hasChild(KEY_COMB_ALT)) {
512 } else {
514 }
515
516 if (keyCombElem->hasChild(KEY_COMB_KEY)) {
517 ObjectState* keyElem = keyCombElem->childByName(KEY_COMB_KEY);
518 string key = keyElem->stringAttribute(KEY_VALUE);
519 char keyChar = 0;
520
521 // delete key requires extra checking
522 if (key == DELETE_KEY) {
523 keyChar = 127; // ASCII 127 = DEL
524 } else {
525 keyChar = *(key.c_str());
526 }
527
528 shortcut->setAttribute(KeyboardShortcut::OSKEY_KEY, keyChar);
529 }
530
531 if (keyCombElem->hasChild(KEY_COMB_FKEY)) {
532 ObjectState* fKeyElem = keyCombElem->childByName(KEY_COMB_FKEY);
533 int number = fKeyElem->intAttribute(FKEY_NUMBER);
535 }
536
537 ObjectState* actionElem = ksElem->childByName(KS_ACTION);
538 string action = actionElem->stringValue();
540}
#define assert(condition)
const string TB_VISIBLE
Name of the element declaring toolbar visibility.
const string KEYBOARD_SHORTCUT
Name of the element declaring a keyboard shortcut.
const string DELETE_KEY
String to mean delete key.
const string KEY_VALUE
Name of the attribute which declares the actual key of the key element.
const string TB_SEPARATOR
Name of the element declaring a toolbar separator.
const int DEFAULT_HEIGHT
Default window height.
const string WI_WIDTH
Name of the element declaring window width.
const string KS_KEY_COMBINATION
Name of the element declaring key combination.
const string FALSE
Value used for false in attribute and element values.
const string SEPARATOR_POSITION
Name of the attribute of the separator element.
const string TRUE
Value used for true in attribute and element values.
const int DEFAULT_Y_POS
Default y position.
const string SLOT_POSITION
Name of the attribute of the slot element.
const string WI_X_POSITION
Name of the element declaring x position of the window.
const string TB_SLOT
Name of the element declaring a toolbar button.
const string KEY_COMB_KEY
Name of the element meaning some other key in key combination.
const string TOOLBAR
Name of the element containing toolbar declaration.
const string TB_LA_ICON
Name of the icon element insie layout element.
const string WINDOW
Name of the element containing window declaration.
const string KEY_COMB_ALT
Name of the element meaning alt key in key combination.
const string WI_Y_POSITION
Name of the element declaring y position of the window.
const string TB_LAYOUT
Name of the element containing toolbar layout declaration.
const string TB_LA_TEXT
Name of the text element inside layout element.
const string KEY_COMB_FKEY
Name of the element meaning some function key in key combination.
const string FKEY_NUMBER
Name of the attribute declaring function key number.
const int DEFAULT_X_POS
Default x position.
const string KS_ACTION
Name of the element declaring the action performed by the keyboard shortcut.
const string TB_SLOT_ACTION
Name of the element declaring action name inside slot element.
const string WI_FULLSCREEN
Name of the full-screen element inside window element.
const string WI_HEIGHT
Name of the element declaring window height.
const string KEY_COMB_CTRL
Name of the element meaning ctrl key in key combination.
const int DEFAULT_WIDTH
Default window width.
static MachInfoCmdLineOptions options
Definition MachInfo.cc:46
static std::string toString(const T &source)
void setToolbarProperties(const ObjectState *toolbarElem, ObjectState *options) const
void writeState(const ObjectState *optionsState)
void setWindowProperties(const ObjectState *windowElem, ObjectState *options) const
void writeOptions(const GUIOptions &options)
virtual ObjectState * convertToOptionsObjectFormat(const ObjectState *root) const
void addKeyboardShortcut(const ObjectState *ksElem, ObjectState *options) const
GUIOptionsSerializer(std::string configurationName)
virtual ObjectState * convertToConfigFileFormat(const ObjectState *options) const
std::string configurationName_
Name of the configuration to read/write.
static const std::string OSKEY_X_POS
ObjectState attribute key for window x position.
static const std::string OSKEY_POSITION
ObjectState attribute key for separator position.
static const std::string OSVALUE_BOTH
ObjectState attribute value for text & icon layout.
static const std::string OSKEY_WINDOW_WIDTH
ObjectState attribute key for window width.
static const std::string OSVALUE_TEXT
ObjectState attribute value for text layout.
static const std::string OSKEY_TOOLBAR_VISIBILITY
ObjectState attribute key for toolbar visibility.
static const std::string OSNAME_OPTIONS
ObjectState name for the options.
static const std::string OSKEY_FULL_SCREEN
ObjectState attribute key for full screen feature.
static const std::string OSKEY_TOOLBAR_LAYOUT
ObjectState attribute key for toolbar layout.
static const std::string OSKEY_WINDOW_HEIGHT
ObjectState attribute key for window height.
static const std::string OSNAME_SEPARATOR
ObjectState name for separator.
static const std::string OSKEY_Y_POS
ObjectState attribute key for window y position.
static const std::string OSVALUE_ICON
ObjectState attribute value for icon layout.
static const std::string OSKEY_KEY
ObjectState attribute key for normal letter or number key.
static const std::string OSKEY_ALT
ObjectState attribute key for alt key.
static const std::string OSKEY_FKEY
ObjectState attribute key for function key number.
static const std::string OSKEY_ACTION
ObjectState attribute key for action name.
static const std::string OSKEY_CTRL
ObjectState attribute key for ctrl key.
static const std::string OSNAME_KEYBOARD_SHORTCUT
ObjectState name for keyboard shortcut.
bool hasAttribute(const std::string &name) const
ObjectState * childByName(const std::string &name) const
void setAttribute(const std::string &name, const std::string &value)
void setValue(const std::string &value)
bool hasChild(const std::string &name) const
ObjectState * child(int index) const
void addChild(ObjectState *child)
std::string stringAttribute(const std::string &name) const
int intAttribute(const std::string &name) const
std::string stringValue() const
std::string name() const
int childCount() const
static const std::string OSKEY_SLOT
ObjectState attribute key for slot position.
static const std::string OSKEY_ACTION
ObjectState attribute key for action name.
static const std::string OSNAME_TOOLBAR_BUTTON
ObjectState name for ToolbarButton.
virtual void writeState(const ObjectState *rootState)
virtual ObjectState * readState()