]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/pages.cpp
1b1201548fa7017ca69ac6cbcb516547fe1460c7
[bacula/bacula] / bacula / src / qt-console / pages.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation plus additions
11    that are listed in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *   Version $Id: batstack.cpp 4230 2007-02-21 20:07:37Z kerns $
30  *
31  *   Dirk Bartley, March 2007
32  */
33
34 #include "pages.h"
35 #include "bat.h"
36
37 /*
38  * dockPage
39  * This function is intended to be called from within the Pages class to pull
40  * a window from floating to in the stack widget.
41  */
42
43 void Pages::dockPage()
44 {
45    /* These two lines are for making sure if it is being changed from a window
46     * that it has the proper window flag and parent.
47     */
48    setWindowFlags(Qt::Widget);
49
50    /* This was being done already */
51    m_parent->addWidget(this);
52
53    /* Set docked flag */
54    m_docked = true;
55    mainWin->stackedWidget->setCurrentWidget(this);
56    /* lets set the page selectors action for docking or undocking */
57    setContextMenuDockText();
58 }
59
60 /*
61  * undockPage
62  * This function is intended to be called from within the Pages class to put
63  * a window from the stack widget to a floating window.
64  */
65
66 void Pages::undockPage()
67 {
68    /* Change from a stacked widget to a normal window */
69    m_parent->removeWidget(this);
70    setWindowFlags(Qt::Window);
71    show();
72    /* Clear docked flag */
73    m_docked = false;
74    /* The window has been undocked, lets change the context menu */
75    setContextMenuDockText();
76 }
77
78 /*
79  * This function is intended to be called with the subclasses.  When it is 
80  * called the specific sublclass does not have to be known to Pages.  When it 
81  * is called this function will change the page from it's current state of being
82  * docked or undocked and change it to the other.
83  */
84
85 void Pages::togglePageDocking()
86 {
87    if (m_docked) {
88       undockPage();
89    } else {
90       dockPage();
91    }
92 }
93
94 /*
95  * This function is because I wanted for some reason to keep it protected but still 
96  * give any subclasses the ability to find out if it is currently stacked or not.
97  */
98 bool Pages::isDocked()
99 {
100    return m_docked;
101 }
102
103 /*
104  * To keep m_closeable protected as well
105  */
106 bool Pages::isCloseable()
107 {
108    return m_closeable;
109 }
110
111 /*
112  * When a window is closed, this slot is called.  The idea is to put it back in the
113  * stack here, and it works.  I wanted to get it to the top of the stack so that the
114  * user immediately sees where his window went.  Also, if he undocks the window, then
115  * closes it with the tree item highlighted, it may be confusing that the highlighted
116  * treewidgetitem is not the stack item in the front.
117  */
118
119 void Pages::closeEvent(QCloseEvent* event)
120 {
121    /* A Widget was closed, lets toggle it back into the window, and set it in front. */
122    dockPage();
123
124    /* this fixes my woes of getting the widget to show up on top when closed */
125    event->ignore();
126
127    /* Set the current tree widget item in the Page Selector window to the item 
128     * which represents "this" 
129     * Which will also bring "this" to the top of the stacked widget */
130    setCurrent();
131 }
132
133 /*
134  * The next three are virtual functions.  The idea here is that each subclass will have the
135  * built in virtual function to override if the programmer wants to populate the window
136  * when it it is first clicked.
137  */
138 void Pages::PgSeltreeWidgetClicked()
139 {
140 }
141
142 /*
143  *  Virtual function which is called when this page is visible on the stack.
144  *  This will be overridden by classes that want to populate if they are on the
145  *  top.
146  */
147 void Pages::currentStackItem()
148 {
149 }
150
151 /*
152  * Function to close the stacked page and remove the widget from the
153  * Page selector window
154  */
155 void Pages::closeStackPage()
156 {
157    /* First get the tree widget item and destroy it */
158    QTreeWidgetItem *item=mainWin->getFromHash(this);
159    /* remove the QTreeWidgetItem <-> page from the hash */
160    mainWin->hashRemove(item, this);
161    /* remove the item from the page selector by destroying it */
162    delete item;
163    /* remove this */
164    delete this;
165 }
166
167 /*
168  * Function to set members from the external mainwin and it's overload being
169  * passed a specific QTreeWidgetItem to be it's parent on the tree
170  */
171 void Pages::pgInitialize()
172 {
173    pgInitialize(NULL);
174 }
175
176 void Pages::pgInitialize(QTreeWidgetItem *parentTreeWidgetItem)
177 {
178    m_parent = mainWin->stackedWidget;
179    m_console = mainWin->currentConsole();
180
181    if (!parentTreeWidgetItem) {
182       parentTreeWidgetItem = m_console->directorTreeItem();
183    }
184
185    QTreeWidgetItem *item = new QTreeWidgetItem(parentTreeWidgetItem);
186    QString name; 
187    treeWidgetName(name);
188    item->setText(0, name);
189    mainWin->hashInsert(item, this);
190    setTitle();
191 }
192
193 /*
194  * Virtual Function to return a name
195  * All subclasses should override this function
196  */
197 void Pages::treeWidgetName(QString &name)
198 {
199    name = m_name;
200 }
201
202 /*
203  * Function to simplify executing a console command and bringing the
204  * console to the front of the stack
205  */
206 void Pages::consoleCommand(QString &command)
207 {
208    /*if (!m_console->is_connectedGui())
209        return;*/
210    if (!m_console->preventInUseConnect())
211        return;
212    /* Bring this directors console to the front of the stack */
213    setConsoleCurrent();
214    QString displayhtml("<font color=\"blue\">");
215    displayhtml += command + "</font>\n";
216    m_console->display_html(displayhtml);
217    m_console->display_text("\n");
218    m_console->write_dir(command.toUtf8().data());
219    m_console->displayToPrompt();
220 }
221
222 /*
223  * Function for handling undocked windows becoming active.
224  * Change the currently selected item in the page selector window to the now
225  * active undocked window.  This will also make the console for the undocked
226  * window m_currentConsole.
227  */
228 void Pages::changeEvent(QEvent *event)
229 {
230    if ((event->type() ==  QEvent::ActivationChange) && (isActiveWindow())) {
231       setCurrent();
232    }
233 }
234
235 /*
236  * Function to simplify getting the name of the class and the director into
237  * the caption or title of the window
238  */
239 void Pages::setTitle()
240 {
241    QString title, director;
242    treeWidgetName(title);
243    m_console->getDirResName(director);
244    title += " of Director ";
245    title += director;
246    setWindowTitle(title);
247 }
248
249 /*
250  * Bring the current directors console window to the top of the stack.
251  */
252 void Pages::setConsoleCurrent()
253 {
254    mainWin->treeWidget->setCurrentItem(mainWin->getFromHash(m_console));
255 }
256
257 /*
258  * Bring this window to the top of the stack.
259  */
260 void Pages::setCurrent()
261 {
262    mainWin->treeWidget->setCurrentItem(mainWin->getFromHash(this));
263 }
264
265 /*
266  * Function to set the text of the toggle dock context menu when page and
267  * widget item are NOT known.  
268  */
269 void Pages::setContextMenuDockText()
270 {
271    QTreeWidgetItem *item = mainWin->getFromHash(this);
272    QString docktext("");
273    if (isDocked()) {
274        docktext += "UnDock ";
275    } else {
276        docktext += "ReDock ";
277    }
278    docktext += item->text(0) += " Window";
279       
280    mainWin->actionToggleDock->setText(docktext);
281    setTreeWidgetItemDockColor();
282 }
283
284 /*
285  * Function to set the color of the tree widget item based on whether it is
286  * docked or not.
287  */
288 void Pages::setTreeWidgetItemDockColor()
289 {
290    QTreeWidgetItem* item = mainWin->getFromHash(this);
291    if (item) {
292       if (item->text(0) != "Console") {
293          if (isDocked()) {
294          /* Set the brush to blue if undocked */
295             QBrush blackBrush(Qt::black);
296             item->setForeground(0, blackBrush);
297          } else {
298          /* Set the brush back to black if docked */
299             QBrush blueBrush(Qt::blue);
300             item->setForeground(0, blueBrush);
301          }
302       }
303    }
304 }