view_controller.hpp 7.32 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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 186 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 223 224 225 226 227 228 229 230 231 232 233 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
#ifndef CVVISUAL_VIEWCONTROLLER_HPP
#define CVVISUAL_VIEWCONTROLLER_HPP

#include <vector>
#include <algorithm>
#include <iostream>
#include <map>
#include <memory>

#include <functional>
#include <utility>
#include <QString>

#include "../util/util.hpp"
#include "../impl/call.hpp"
#include "../gui/call_window.hpp"
#include "../gui/call_tab.hpp"


namespace cvv
{

namespace gui
{
class CallTab;
class CallWindow;
class MainCallWindow;
class OverviewPanel;
}

namespace controller
{

/**
 * @brief Modes that this cvv application can be running in.
 */
enum class Mode
{
	/**
         * @brief The normal mode.
         */
	NORMAL = 0,
	/**
         * @brief The cvv UI is hidden.
         */
	HIDE = 1,
	/**
         * @brief The cvv UI stops only at the final call
         * The final call is the call which is called after `cvv::finalShow()`)
         */
	FAST_FORWARD = 2
};

class ViewController;

/**
 * @brief Typedef for a function that creates a CallTab from a impl::Call.
 */
using TabFactory =
    std::function<std::unique_ptr<gui::CallTab>(util::Reference<impl::Call>)>;

/**
 * @brief Controlls the windows, call tabs and the event fetch loop.
 * Its the layer between the low level model (aka DataController) an the high
 * level GUI (aka CallTab, OverviewPanel, ...).
 */
class ViewController
{
      public:
	/**
	 * @brief The default contructor for this class.
	 */
	ViewController();

	/**
	 * @brief Clean up.
	 */
	~ViewController();

	/**
	 * @brief Adds the new call tab type.
	 * @param typeName name of the new type
	 * @param constr function constructing an instance of this  call tab
	 * type
	 * @return an instance of the new call tab type
	 */
	static void addCallType(const QString typeName, TabFactory constr);

	/**
	 * @brief Adds a new call and shows it in the overview table.
	 * @param data new call (data)
	 */
	void addCall(util::Reference<impl::Call> data);

	/**
	 * @brief Execute the Qt event loop.
	 */
	void exec();

	/**
	 * @brief Get the call with the given id.
	 * @param id given id
	 * @return call with the given id
	 */
	impl::Call &getCall(size_t id);

	/**
	 * @brief Get the current setting [key] in the given scope.
	 * Please use `setDefaultSetting` to set a default value that's other
	 * than
	 * an empty QString.
	 * @param scope given scope (e.g. 'Overview')
	 * @param key settings key (e.g. 'autoOpenTabs')
	 * @return settings string
	 */
	QString getSetting(const QString &scope, const QString &key);

	/**
	 * @brief Get the inherited call windows with tabs.
	 * @return the inherited CallWindows
	 */
	std::vector<util::Reference<gui::CallWindow>> getTabWindows();

	/**
	 * @brief Get the inherited main window.
	 * @return the inherited main window
	 */
	util::Reference<gui::MainCallWindow> getMainWindow();

	/**
	 * @brief Move the call tab with the given id to a new window.
	 * @param tabId given call tab id
	 */
	void moveCallTabToNewWindow(size_t tabId);

	/**
	 * @brief Move the given call tab to the given window.
	 * @param tabId id of the given call tab
	 * @param windowId id of the given window (0 is the main window)
	 */
	void moveCallTabToWindow(size_t tabId, size_t windowId);

	/**
	 * @brief Removes the call tab with the given id.
	 * @param tabId given id
	 * @param deleteCall if deleteCall and deleteIt are true, it also
	 * deletes the proper Call
	 */
	void removeCallTab(size_t tabId, bool deleteIt = true,
	                   bool deleteCall = false, bool updateUI = true);

	/**
	 * @brief Opens the users default browser with the topic help page.
	 * Current URL: cvv.mostlynerdless.de/help.php?topic=[topic]
	 *
	 * Topics can be added via appending the doc/topics.yml file.
	 *
	 * @param topic help topic
	 */
	void openHelpBrowser(const QString &topic);

	/**
	 * @brief Resume the execution of the calling program.
	 */
	void resumeProgramExecution();

	/**
	 * @brief Set the default setting for a given stettings key and scope.
	 * It doesn't override existing settings.
	 * @param scope given settings scope
	 * @param key given settings key
	 * @param value default value of the setting
	 */
	void setDefaultSetting(const QString &scope, const QString &key,
	                       const QString &value);

	/**
	 * @brief Set the setting for a given stettings key and scope.
	 * @param scope given settings scope
	 * @param key given settings key
	 * @param value new value of the setting
	 */
	void setSetting(const QString &scope, const QString &key,
	                const QString &value);

	/**
	 * @brief Show the given call tab and bring it's window to the front.
	 * @note It's not guaranteed that it really brings the tabs' window to the front.
	 * @param tabId id of the given call tab
	 */
	void showCallTab(size_t tabId);

	/**
	 * @brief Shows the tab and opens it if neccessary.
	 * @param tabId id of the tab
	 */
	void showAndOpenCallTab(size_t tabId);
	
	/**
	 * @brief Opens the tab it if neccessary.
	 * @param tabId id of the tab
	 */
	void openCallTab(size_t tabId);

	/**
	 * @brief Show the overview tab (and table) and bring it's window to the
	 * front.
	 * @note The latter is not guaranteed.
	 */
	void showOverview();

	/**
	 * @brief Get the window in which the given tab lays currently.
	 * @param tabId id of the given call tab
	 * @return current window
	 */
	gui::CallWindow *getCurrentWindowOfTab(size_t tabId);

	/**
	 * @brief Returns the call tab with the given id and constructs it if
	 * doesn't exit.
	 * @param tabId given id
	 * @return call tab with given id
	 */
	gui::CallTab *getCallTab(size_t tabId);

	/**
	 * @brief Remove the window from the internal data structures.
	 * @param windowId id of the window
	 * @note Only call this method if you now the implacations of deleting
	 * the window.
	 */
	void removeWindowFromMaps(size_t windowId);

	/**
	 * @brief Shows an "Exit program" button on each window.
	 */
	void showExitProgramButton();

	/**
	 * @brief Removes the empty windows.
	 * @note It's safer to call the removeEmptyWindowsWithDelay method
	 * instead.
	 */
	void removeEmptyWindows();

	/**
	 * @brief Removes the empty windows with a small delay.
	 */
	void removeEmptyWindowsWithDelay();

	/**
	 * @brief Checks whether or not is useful to call the
	 * removeEmptyWindows() method.
	 * @return Is is useful to call the removeEmptyWindows() method?
	 * @note Please don't call this method outside a periodcally called
	 * method.
	 */
	bool shouldRunRemoveEmptyWindows();

	/**
	 * @brief Set the mode that this application is running in.
	 * @param newMode mode to be set
	 */
	void setMode(Mode newMode);

	/**
	 * @brief Returns the mode this program is running in.
	 * @return the current mode, NROMAL, HIDE or FAST_FORWARD
	 */
	Mode getMode();

	/**
	 * @brief Checks whether or not the `cvv::finalCall()` method has been
	 * called?
	 * @return Has the `cvv::finalCall()` method been called?
	 */
	bool hasFinalCall();

      private:
	static std::map<QString, TabFactory> callTabType;

	std::map<size_t, std::unique_ptr<gui::CallWindow>> windowMap{};
	gui::MainCallWindow *mainWindow;

	std::map<size_t, std::unique_ptr<gui::CallTab>> callTabMap{};
	gui::OverviewPanel *ovPanel;
	bool doesShowExitProgramButton = false;
	/**
	 * @brief Counter == 0 <=> you should run `removeEmptyWindows()`.
	 */
	bool shouldRunRemoveEmptyWindows_ = true;

	Mode mode = Mode::NORMAL;

	bool ownsQApplication = false;

	size_t max_window_id = 0;

	bool hasCall(size_t id);

	void updateMode();

	void hideAll();
};
}
}

#endif