]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/pages.cpp
This is a patch from Riccardo. It is for updating translations.
[bacula/bacula] / bacula / src / qt-console / pages.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 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 and included
11    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$
30  *
31  *   Dirk Bartley, March 2007
32  */
33
34 #include "pages.h"
35 #include "bat.h"
36
37 /* A global function */
38 bool isWin32Path(QString &fullPath) 
39 {
40    char *buf = fullPath.left(2).toUtf8().data();
41
42    return buf[1] == ':' && B_ISALPHA(buf[0]);
43 }
44
45
46 /*
47  * dockPage
48  * This function is intended to be called from within the Pages class to pull
49  * a window from floating to in the stack widget.
50  */
51
52 void Pages::dockPage()
53 {
54    /* These two lines are for making sure if it is being changed from a window
55     * that it has the proper window flag and parent.
56     */
57    setWindowFlags(Qt::Widget);
58
59    /* This was being done already */
60    m_parent->addWidget(this);
61
62    /* Set docked flag */
63    m_docked = true;
64    mainWin->stackedWidget->setCurrentWidget(this);
65    /* lets set the page selectors action for docking or undocking */
66    setContextMenuDockText();
67 }
68
69 /*
70  * undockPage
71  * This function is intended to be called from within the Pages class to put
72  * a window from the stack widget to a floating window.
73  */
74
75 void Pages::undockPage()
76 {
77    /* Change from a stacked widget to a normal window */
78    m_parent->removeWidget(this);
79    setWindowFlags(Qt::Window);
80    show();
81    /* Clear docked flag */
82    m_docked = false;
83    /* The window has been undocked, lets change the context menu */
84    setContextMenuDockText();
85 }
86
87 /*
88  * This function is intended to be called with the subclasses.  When it is 
89  * called the specific sublclass does not have to be known to Pages.  When it 
90  * is called this function will change the page from it's current state of being
91  * docked or undocked and change it to the other.
92  */
93
94 void Pages::togglePageDocking()
95 {
96    if (m_docked) {
97       undockPage();
98    } else {
99       dockPage();
100    }
101 }
102
103 /*
104  * This function is because I wanted for some reason to keep it protected but still 
105  * give any subclasses the ability to find out if it is currently stacked or not.
106  */
107 bool Pages::isDocked()
108 {
109    return m_docked;
110 }
111
112 /*
113  * To keep m_closeable protected as well
114  */
115 bool Pages::isCloseable()
116 {
117    return m_closeable;
118 }
119
120 /*
121  * When a window is closed, this slot is called.  The idea is to put it back in the
122  * stack here, and it works.  I wanted to get it to the top of the stack so that the
123  * user immediately sees where his window went.  Also, if he undocks the window, then
124  * closes it with the tree item highlighted, it may be confusing that the highlighted
125  * treewidgetitem is not the stack item in the front.
126  */
127
128 void Pages::closeEvent(QCloseEvent* event)
129 {
130    /* A Widget was closed, lets toggle it back into the window, and set it in front. */
131    dockPage();
132
133    /* this fixes my woes of getting the widget to show up on top when closed */
134    event->ignore();
135
136    /* Set the current tree widget item in the Page Selector window to the item 
137     * which represents "this" 
138     * Which will also bring "this" to the top of the stacked widget */
139    setCurrent();
140 }
141
142 /*
143  * The next three are virtual functions.  The idea here is that each subclass will have the
144  * built in virtual function to override if the programmer wants to populate the window
145  * when it it is first clicked.
146  */
147 void Pages::PgSeltreeWidgetClicked()
148 {
149 }
150
151 /*
152  *  Virtual function which is called when this page is visible on the stack.
153  *  This will be overridden by classes that want to populate if they are on the
154  *  top.
155  */
156 void Pages::currentStackItem()
157 {
158 }
159
160 /*
161  * Function to close the stacked page and remove the widget from the
162  * Page selector window
163  */
164 void Pages::closeStackPage()
165 {
166    /* First get the tree widget item and destroy it */
167    QTreeWidgetItem *item=mainWin->getFromHash(this);
168    /* remove the QTreeWidgetItem <-> page from the hash */
169    mainWin->hashRemove(item, this);
170    /* remove the item from the page selector by destroying it */
171    delete item;
172    /* remove this */
173    delete this;
174 }
175
176 /*
177  * Function to set members from the external mainwin and it's overload being
178  * passed a specific QTreeWidgetItem to be it's parent on the tree
179  */
180 void Pages::pgInitialize()
181 {
182    pgInitialize(NULL);
183 }
184
185 void Pages::pgInitialize(QTreeWidgetItem *parentTreeWidgetItem)
186 {
187    m_parent = mainWin->stackedWidget;
188    m_console = mainWin->currentConsole();
189
190    if (!parentTreeWidgetItem) {
191       parentTreeWidgetItem = m_console->directorTreeItem();
192    }
193
194    QTreeWidgetItem *item = new QTreeWidgetItem(parentTreeWidgetItem);
195    QString name; 
196    treeWidgetName(name);
197    item->setText(0, name);
198    mainWin->hashInsert(item, this);
199    setTitle();
200 }
201
202 /*
203  * Virtual Function to return a name
204  * All subclasses should override this function
205  */
206 void Pages::treeWidgetName(QString &name)
207 {
208    name = m_name;
209 }
210
211 /*
212  * Function to simplify executing a console command and bringing the
213  * console to the front of the stack
214  */
215 void Pages::consoleCommand(QString &command)
216 {
217    /*if (!m_console->is_connectedGui())
218        return;*/
219    if (!m_console->preventInUseConnect()) {
220        return;
221    }
222    consoleInput(command);
223 }
224
225 /*
226  * Function to simplify executing a console command, but does not
227  *  check for the connection in use.  We need this so that we can
228  *  *always* enter command from the command line.
229  */
230 void Pages::consoleInput(QString &command)
231 {
232    /* Bring this director's console to the front of the stack */
233    setConsoleCurrent();
234    QString displayhtml("<font color=\"blue\">");
235    displayhtml += command + "</font>\n";
236    m_console->display_html(displayhtml);
237    m_console->display_text("\n");
238    m_console->write_dir(command.toUtf8().data());
239    m_console->displayToPrompt();
240 }
241
242 /*
243  * Function for handling undocked windows becoming active.
244  * Change the currently selected item in the page selector window to the now
245  * active undocked window.  This will also make the console for the undocked
246  * window m_currentConsole.
247  */
248 void Pages::changeEvent(QEvent *event)
249 {
250    if ((event->type() ==  QEvent::ActivationChange) && (isActiveWindow())) {
251       setCurrent();
252    }
253 }
254
255 /*
256  * Function to simplify getting the name of the class and the director into
257  * the caption or title of the window
258  */
259 void Pages::setTitle()
260 {
261    QString wdgname, director;
262    treeWidgetName(wdgname);
263    m_console->getDirResName(director);
264    QString title = tr("%1 of Director %2").arg(wdgname).arg(director);
265    setWindowTitle(title);
266 }
267
268 /*
269  * Bring the current directors console window to the top of the stack.
270  */
271 void Pages::setConsoleCurrent()
272 {
273    mainWin->treeWidget->setCurrentItem(mainWin->getFromHash(m_console));
274 }
275
276 /*
277  * Bring this window to the top of the stack.
278  */
279 void Pages::setCurrent()
280 {
281    mainWin->treeWidget->setCurrentItem(mainWin->getFromHash(this));
282 }
283
284 /*
285  * Function to set the text of the toggle dock context menu when page and
286  * widget item are NOT known.  
287  */
288 void Pages::setContextMenuDockText()
289 {
290    QTreeWidgetItem *item = mainWin->getFromHash(this);
291    QString docktext;
292    if (isDocked()) {
293       docktext = tr("UnDock %1 Window").arg(item->text(0));
294    } else {
295       docktext = tr("ReDock %1 Window").arg(item->text(0));
296    }
297       
298    mainWin->actionToggleDock->setText(docktext);
299    setTreeWidgetItemDockColor();
300 }
301
302 /*
303  * Function to set the color of the tree widget item based on whether it is
304  * docked or not.
305  */
306 void Pages::setTreeWidgetItemDockColor()
307 {
308    QTreeWidgetItem* item = mainWin->getFromHash(this);
309    if (item) {
310       if (item->text(0) != tr("Console")) {
311          if (isDocked()) {
312          /* Set the brush to blue if undocked */
313             QBrush blackBrush(Qt::black);
314             item->setForeground(0, blackBrush);
315          } else {
316          /* Set the brush back to black if docked */
317             QBrush blueBrush(Qt::blue);
318             item->setForeground(0, blueBrush);
319          }
320       }
321    }
322 }