Report error conditions instead of just doing nothing.
[tagua/yd.git] / src / ui.h
blobc5d47777b9d35fe9a4f7d0b74adc75d6dacfc324
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
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 */
11 #ifndef UI_H
12 #define UI_H
14 #include <boost/shared_ptr.hpp>
15 #include <map>
16 #include <core/state_fwd.h>
17 #include <QObject>
18 #include <KUrl>
19 #include "common.h"
20 #include "controllers/entitytoken.h"
22 class ActionCollection;
23 class ActionStateObserver;
24 class Controller;
25 class Engine;
26 class KActionCollection;
27 class IColor;
28 class PGN;
30 /**
31 * @brief Utility class to handle GUI actions.
33 * An instance of UI is owned by MainWindow, and every GUI action
34 * is connected to a slot of UI.
35 * A UI instance maintains a correspondence between Tagua tabs and
36 * associated controllers. Besides, UI knows the active tab (hence
37 * controller) and directs all user interaction to it.
38 * Whenever a new tab is created, the corresponding controller is
39 * added to the association, and its setUI member function is called,
40 * so that it can setup all the observers.
42 * To add a new tab <-> controller association, use the addController
43 * member function.
44 * If you want to replace the current controller, use setController.
45 * Both functions assume that the controller is new, hence initialize
46 * it via setUI.
48 class UI : public QObject {
49 Q_OBJECT
50 typedef std::map<QWidget*, boost::shared_ptr<Controller> > ControllerMap;
51 ControllerMap m_controller;
52 QWidget* m_current_tab;
53 boost::shared_ptr<Controller>& controller();
54 boost::shared_ptr<Controller> controller() const;
55 KActionCollection* m_actions;
56 KUrl m_url;
58 friend class UIActionStateObserver;
59 public:
60 /**
61 * Constructor.
62 * \param actions The action collection associated to this UI instance.
63 * This will be used to enable/disable actions according
64 * to notifications coming from the controller.
66 UI(KActionCollection* actions);
68 /**
69 * Add a new tab <-> controller association.
70 * The controller is assumed to be new, and it is initialized by calling
71 * its setUI member function.
73 void addController(QWidget* tab, const boost::shared_ptr<Controller>&);
74 /**
75 * Just like addController, but replace the current controller instead
76 * of creating a new association. The old controller is no longer referenced
77 * by UI, so it will be destroyed, unless other objects keep it alive.
79 void setController(const boost::shared_ptr<Controller>& controller);
80 /**
81 * Remove a controller. The controller will no longer be referenced by
82 * UI, so it will be destroyed, unless other objects keep it alive.
84 void removeController(QWidget* tab);
86 /**
87 * Create an action state observer that reacts to action state
88 * updates by enabling / disabling the corresponding actions.
89 * \sa ActionStateObserver
91 boost::shared_ptr<ActionStateObserver>
92 createActionStateObserver(const boost::shared_ptr<Controller>&) const;
93 public Q_SLOTS:
94 /**
95 * Change the current tab and associated controller.
97 void setCurrentTab(QWidget* tab);
99 bool undo();
100 bool redo();
101 bool truncate();
102 bool promoteVariation();
103 bool back();
104 bool forward();
105 void gotoFirst();
106 void gotoLast();
107 void setTurn(int);
108 void createCtrlAction();
109 void destroyCtrlAction();
110 ActionCollection* variantActions() const;
112 void pgnCopy();
113 void pgnPaste();
114 void pgnPaste(const QString&);
115 void pgnPaste(const PGN& pgn);
116 QString currentPGN();
118 // editing
119 void clearBoard();
120 void setStartingPosition();
121 void copyPosition();
122 void pastePosition();
123 StatePtr position() const;
125 EntityToken addPlayingEngine(const IColor* side, const boost::shared_ptr<Engine>& engine);
126 // EntityToken addAnalysingEngine(const boost::shared_ptr<Engine>& engine);
127 void removeEntity(const EntityToken& token);
129 void end();
130 void detach();
132 QString currentVariant() const;
133 void reloadSettings();
135 KUrl url() const;
136 void setUrl(const KUrl& url);
139 #endif