]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mainwin.cpp
kes Add context menu for floating a window.
[bacula/bacula] / bacula / src / qt-console / mainwin.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-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 /*
30  *   Version $Id$
31  *
32  *  Main Window control for bat (qt-console)
33  *
34  *   Kern Sibbald, January MMVII
35  *
36  */ 
37
38 #include "bat.h"
39 #include "medialist/medialist.h"
40
41 MainWin::MainWin(QWidget *parent) : QMainWindow(parent)
42 {
43
44    mainWin = this;
45    setupUi(this);                     /* Setup UI defined by main.ui (designer) */
46    treeWidget->clear();
47    treeWidget->setColumnCount(1);
48    treeWidget->setHeaderLabel("Select Page");
49    treeWidget->addAction(actionPullWindowOut);
50    treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
51
52    m_pages = 0;
53    createPages();
54
55    resetFocus();
56
57    createConnections();
58
59    this->show();
60
61    readSettings();
62
63    m_console->connect();
64 }
65
66 void MainWin::createPages()
67 {
68    DIRRES *dir;
69    QTreeWidgetItem *item, *topItem;
70
71    /* Create console tree stacked widget item */
72    m_console = new Console(stackedWidget);
73
74    /* Console is special -> needs director*/
75    /* Just take the first Director */
76    LockRes();
77    dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
78    m_console->setDirRes(dir);
79    UnlockRes();
80
81    /* The top tree item representing the director */
82    topItem = createTopPage(dir->name());
83    topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
84
85    /* Create Tree Widget Item */
86    item = createPage("Console", topItem);
87    m_console->SetPassedValues(stackedWidget, item, m_pages++ );
88
89    /* Append to bstacklist */
90    m_bstacklist.append(m_console);
91
92    /* Set BatStack m_treeItem */
93    QBrush redBrush(Qt::red);
94    item->setForeground(0, redBrush);
95
96    /*
97     * Now with the console created, on with the rest, these are easy   
98     * All should be
99     * 1. create tree widget item
100     * 2. create object passing pointer to tree widget item (modified constructors to pass QTreeWidget pointers)
101     * 3. append to stacklist
102     * And it can even be done in one line.
103     */
104
105    /* brestore */
106    m_bstacklist.append(new bRestore(stackedWidget, 
107                        createPage("brestore", topItem), m_pages++ ));
108
109    /* lastly for now, the medialist */
110    m_bstacklist.append(new MediaList(stackedWidget, m_console, 
111                        createPage("Storage Tree", topItem ), m_pages++));
112
113    /* Iterate through and add to the stack */
114    for (QList<BatStack*>::iterator bstackItem = m_bstacklist.begin(); 
115          bstackItem != m_bstacklist.end(); ++bstackItem ) {
116       (*bstackItem)->AddTostack();
117    }
118
119    treeWidget->expandItem(topItem);
120    stackedWidget->setCurrentIndex(0);
121 }
122
123 /* Create a root Tree Widget */
124 QTreeWidgetItem *MainWin::createTopPage(char *name)
125 {
126    QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
127    item->setText(0, name);
128    return item;
129 }
130
131 /* Create A Tree Widget Item which will be associated with a Page in the stacked widget */
132 QTreeWidgetItem *MainWin::createPage(char *name, QTreeWidgetItem *parent)
133 {
134    QTreeWidgetItem *item = new QTreeWidgetItem(parent);
135    item->setText(0, name);
136    return item;
137 }
138
139 /*
140  * Handle up and down arrow keys for the command line
141  *  history.
142  */
143 void MainWin::keyPressEvent(QKeyEvent *event)
144 {
145    if (m_cmd_history.size() == 0) {
146       event->ignore();
147       return;
148    }
149    switch (event->key()) {
150    case Qt::Key_Down:
151       if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
152          event->ignore();
153          return;
154       }
155       m_cmd_last++;
156       break;
157    case Qt::Key_Up:
158       if (m_cmd_last == 0) {
159          event->ignore();
160          return;
161       }
162       if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
163          m_cmd_last = m_cmd_history.size() - 1;
164       } else {
165          m_cmd_last--;
166       }
167       break;
168    default:
169       event->ignore();
170       return;
171    }
172    lineEdit->setText(m_cmd_history[m_cmd_last]);
173 }
174
175 void MainWin::createConnections()
176 {
177    /* Connect signals to slots */
178    connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
179    connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
180
181 #ifdef xxx
182      connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem *, int)), this, 
183            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
184    connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this, 
185            SLOT(treeItemClicked(QTreeWidgetItem *, int)));  
186 #endif
187    connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, 
188            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
189    connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, 
190            SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
191
192    connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
193    connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
194    connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
195    connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
196    connect(actionLabel, SIGNAL(triggered()), this,  SLOT(labelDialogClicked()));
197    connect(actionRun, SIGNAL(triggered()), this,  SLOT(runDialogClicked()));
198    connect(actionRestore, SIGNAL(triggered()), this,  SLOT(restoreDialogClicked()));
199    connect(actionPullWindowOut, SIGNAL(triggered()), this,  SLOT(floatWindowButton()));
200 }
201
202 /* 
203  * Reimplementation of QWidget closeEvent virtual function   
204  */
205 void MainWin::closeEvent(QCloseEvent *event)
206 {
207    writeSettings();
208    m_console->writeSettings();
209    m_console->terminate();
210    event->accept();
211 }
212
213 void MainWin::writeSettings()
214 {
215    QSettings settings("bacula.org", "bat");
216
217    settings.beginGroup("MainWin");
218    settings.setValue("winSize", size());
219    settings.setValue("winPos", pos());
220    settings.endGroup();
221 }
222
223 void MainWin::readSettings()
224
225    QSettings settings("bacula.org", "bat");
226
227    settings.beginGroup("MainWin");
228    resize(settings.value("winSize", QSize(1041, 801)).toSize());
229    move(settings.value("winPos", QPoint(200, 150)).toPoint());
230    settings.endGroup();
231 }
232
233 /*
234  * This subroutine is called with an item in the Page Selection window
235  *   is clicked 
236  */
237 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
238 {
239    /* Use tree item's Qt::UserRole to get treeindex */
240    int treeindex = item->data(column, Qt::UserRole).toInt();
241    int stackindex=stackedWidget->indexOf(m_bstacklist[treeindex]);
242
243    if( stackindex >= 0 ){
244       stackedWidget->setCurrentIndex(stackindex);
245    }
246 }
247
248 /*
249  * This subroutine is called with an item in the Page Selection window
250  *   is double clicked
251  */
252 void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
253 {
254    int treeindex = item->data(column, Qt::UserRole).toInt();
255
256    /* Use tree item's Qt::UserRole to get treeindex */
257    if ( m_bstacklist[treeindex]->isStacked() == true ){
258       m_bstackpophold=m_bstacklist[treeindex];
259
260       /* Create a popup menu before floating window */
261       QMenu *popup = new QMenu( treeWidget );
262       connect(popup->addAction("Float Window"), SIGNAL(triggered()), this, 
263               SLOT(floatWindow()));
264       popup->exec(QCursor::pos());
265    } else {
266       /* Just pull it back in without prompting */
267       m_bstacklist[treeindex]->Togglestack();
268    }
269 }
270
271 void MainWin::labelDialogClicked() 
272 {
273    new labelDialog(m_console);
274 }
275
276 void MainWin::runDialogClicked() 
277 {
278    new runDialog(m_console);
279 }
280
281 void MainWin::restoreDialogClicked() 
282 {
283    new prerestoreDialog(m_console);
284 }
285
286
287
288 /*
289  * The user just finished typing a line in the command line edit box
290  */
291 void MainWin::input_line()
292 {
293    QString cmdStr = lineEdit->text();    /* Get the text */
294    lineEdit->clear();                    /* clear the lineEdit box */
295    if (m_console->is_connected()) {
296       m_console->display_text(cmdStr + "\n");
297       m_console->write_dir(cmdStr.toUtf8().data());         /* send to dir */
298    } else {
299       set_status("Director not connected. Click on connect button.");
300    }
301    m_cmd_history.append(cmdStr);
302    m_cmd_last = -1;
303 }
304
305
306 void MainWin::about()
307 {
308    QMessageBox::about(this, tr("About bat"),
309             tr("<br><h2>bat 0.2, by Kern Sibbald</h2>"
310             "<p>Copyright &copy; " BYEAR " Free Software Foundation Europe e.V."
311             "<p>The <b>bat</b> is an administrative console"
312                " interface to the Director."));
313 }
314
315 void MainWin::set_statusf(const char *fmt, ...)
316 {
317    va_list arg_ptr;
318    char buf[1000];
319    int len;
320    va_start(arg_ptr, fmt);
321    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
322    va_end(arg_ptr);
323    set_status(buf);
324 }
325
326 void MainWin::set_status_ready()
327 {
328    set_status(" Ready");
329 }
330
331 void MainWin::set_status(const char *buf)
332 {
333    statusBar()->showMessage(buf);
334 }
335
336 void MainWin::floatWindow()
337 {
338    m_bstackpophold->Togglestack();
339 }
340
341 void MainWin::floatWindowButton()
342 {
343    int curindex = stackedWidget->currentIndex();
344    QList<BatStack*>::iterator bstackItem = m_bstacklist.begin();
345    
346    while ((bstackItem != m_bstacklist.end())){
347       if (curindex == stackedWidget->indexOf(*bstackItem)) {
348           (*bstackItem)->Togglestack();
349          break;
350       }
351       ++bstackItem;
352    }
353 }