VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/settings/UISettingsDefs.h@ 37126

Last change on this file since 37126 was 37051, checked in by vboxsync, 13 years ago

FE/Qt: 4989: Lazy init VM settings dialog: Erase cache before loading data.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/** @file
2 *
3 * VBox frontends: Qt GUI ("VirtualBox"):
4 * Header with definitions and functions related to settings dialog
5 */
6
7/*
8 * Copyright (C) 2011 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#ifndef __UISettingsDefs_h__
20#define __UISettingsDefs_h__
21
22/* Qt includes: */
23#include <QPair>
24#include <QMap>
25
26/* VBox includes: */
27#include "COMDefs.h"
28
29/* Settings dialog namespace: */
30namespace UISettingsDefs
31{
32 /* Settings dialog types: */
33 enum SettingsDialogType
34 {
35 SettingsDialogType_Wrong,
36 SettingsDialogType_Offline,
37 SettingsDialogType_Saved,
38 SettingsDialogType_Online
39 };
40
41 /* Machine state => Settings dialog type converter: */
42 SettingsDialogType machineStateToSettingsDialogType(KMachineState machineState);
43}
44
45/* Template to operate settings cache item: */
46template <class CacheData> class UISettingsCache
47{
48public:
49
50 /* Creates empty cache item: */
51 UISettingsCache() { m_value = qMakePair(CacheData(), CacheData()); }
52
53 /* Returns the NON-modifiable REFERENCE to the initial cached data: */
54 const CacheData& base() const { return m_value.first; }
55 /* Returns the NON-modifiable REFERENCE to the current cached data: */
56 const CacheData& data() const { return m_value.second; }
57
58 /* We assume that old cache item was removed if
59 * initial data was set but current data was NOT set.
60 * Returns 'true' if that cache item was removed: */
61 virtual bool wasRemoved() const { return base() != CacheData() && data() == CacheData(); }
62
63 /* We assume that new cache item was created if
64 * initial data was NOT set but current data was set.
65 * Returns 'true' if that cache item was created: */
66 virtual bool wasCreated() const { return base() == CacheData() && data() != CacheData(); }
67
68 /* We assume that old cache item was updated if
69 * current and initial data were both set and not equal to each other.
70 * Returns 'true' if that cache item was updated: */
71 virtual bool wasUpdated() const { return base() != CacheData() && data() != CacheData() && data() != base(); }
72
73 /* We assume that old cache item was actually changed if
74 * 1. this item was removed or
75 * 2. this item was created or
76 * 3. this item was updated.
77 * Returns 'true' if that cache item was actually changed: */
78 virtual bool wasChanged() const { return wasRemoved() || wasCreated() || wasUpdated(); }
79
80 /* Set initial cache item data: */
81 void cacheInitialData(const CacheData &initialData) { m_value.first = initialData; }
82 /* Set current cache item data: */
83 void cacheCurrentData(const CacheData &currentData) { m_value.second = currentData; }
84
85 /* Reset the initial and the current data to be both empty: */
86 void clear() { m_value.first = CacheData(); m_value.second = CacheData(); }
87
88private:
89
90 /* Data: */
91 QPair<CacheData, CacheData> m_value;
92};
93
94/* Template to operate settings cache item with children: */
95template <class ParentCacheData, class ChildCacheData> class UISettingsCachePool : public UISettingsCache<ParentCacheData>
96{
97public:
98
99 /* Typedefs: */
100 typedef QMap<QString, ChildCacheData> UISettingsCacheChildMap;
101 typedef QMapIterator<QString, ChildCacheData> UISettingsCacheChildIterator;
102
103 /* Creates empty cache item: */
104 UISettingsCachePool() : UISettingsCache<ParentCacheData>() {}
105
106 /* Returns the modifiable REFERENCE to the particular child cached data.
107 * If child with such key or index is NOT present,
108 * both those methods will create the new one to return: */
109 ChildCacheData& child(const QString &strChildKey) { return m_children[strChildKey]; }
110 ChildCacheData& child(int iIndex) { return child(indexToKey(iIndex)); }
111
112 /* Returns the NON-modifiable COPY to the particular child cached data.
113 * If child with such key or index is NOT present,
114 * both those methods will create the new one to return: */
115 const ChildCacheData child(const QString &strChildKey) const { return m_children[strChildKey]; }
116 const ChildCacheData child(int iIndex) const { return child(indexToKey(iIndex)); }
117
118 /* Children count: */
119 int childCount() const { return m_children.size(); }
120
121 /* We assume that old cache item was updated if
122 * current and initial data were both set and not equal to each other.
123 * Takes into account all the children.
124 * Returns 'true' if that cache item was updated: */
125 bool wasUpdated() const
126 {
127 /* First of all, cache item is considered to be updated if parent data was updated: */
128 bool fWasUpdated = UISettingsCache<ParentCacheData>::wasUpdated();
129 /* If parent data was NOT updated but also was NOT created or removed too (e.j. was NOT changed at all),
130 * we have to check children too: */
131 if (!fWasUpdated && !UISettingsCache<ParentCacheData>::wasRemoved() && !UISettingsCache<ParentCacheData>::wasCreated())
132 {
133 for (int iChildIndex = 0; !fWasUpdated && iChildIndex < childCount(); ++iChildIndex)
134 if (child(iChildIndex).wasChanged())
135 fWasUpdated = true;
136 }
137 return fWasUpdated;
138 }
139
140 /* Reset the initial and the current data to be both empty.
141 * Removes all the children: */
142 void clear()
143 {
144 UISettingsCache<ParentCacheData>::clear();
145 m_children.clear();
146 }
147
148private:
149
150 QString indexToKey(int iIndex) const
151 {
152 UISettingsCacheChildIterator childIterator(m_children);
153 for (int iChildIndex = 0; childIterator.hasNext(); ++iChildIndex)
154 {
155 childIterator.next();
156 if (iChildIndex == iIndex)
157 return childIterator.key();
158 }
159 return QString::number(iIndex);
160 }
161
162 /* Children: */
163 UISettingsCacheChildMap m_children;
164};
165
166#endif /* __UISettingsDefs_h__ */
167
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use