]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/qt-console/mainwin.cpp
kes Apply fix from for building wx-console on the Mac from
[bacula/bacula] / bacula / src / qt-console / mainwin.cpp
index 7df2cb222bedd156b39dc22df8ad6f39ecd48a8b..6bfcb67a19589189473d1085b3f6a026d1c770f6 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
+   Copyright (C) 2007-2007 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
 */
 
 /*
+ *   Version $Id$
+ *
  *  Main Window control for bat (qt-console)
  *
- *   Kern Sibbald, January MMVI
+ *   Kern Sibbald, January MMVII
  *
  */ 
 
 
 MainWin::MainWin(QWidget *parent) : QMainWindow(parent)
 {
+
    mainWin = this;
    setupUi(this);                     /* Setup UI defined by main.ui (designer) */
-   stackedWidget->setCurrentIndex(0);
 
-   m_console = new Console();
+   createStackedWidgets();
+
+   resetFocus();
+
+   createConnections();
 
-   /* Dummy message ***FIXME*** remove a bit later */
-   textEdit->setPlainText("Hello Baculites\nThis is the main console window.");
-   lineEdit->setFocus();
+   this->show();
 
+   readSettings();
+
+   m_console->connect();
+}
+
+void MainWin::createStackedWidgets()
+{
+   QTreeWidgetItem *item, *topItem;
+   m_console = new Console(stackedWidget);
+   stackedWidget->addWidget(m_console);
+
+   bRestore *brestore = new bRestore(stackedWidget);
+   stackedWidget->addWidget(brestore);
+
+   /* Just take the first Director */
+   LockRes();
+   DIRRES *dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
+   m_console->setDirRes(dir);
+   UnlockRes();
+
+   /* ***FIXME*** Dummy setup of treeWidget */
+   treeWidget->clear();
+   treeWidget->setColumnCount(1);
+   treeWidget->setHeaderLabel("Selection");
+   topItem = new QTreeWidgetItem(treeWidget);
+   topItem->setText(0, dir->name());
+   topItem->setIcon(0, QIcon(QString::fromUtf8("images/server.png")));
+   item = new QTreeWidgetItem(topItem);
+   m_console->setTreeItem(item);
+   item->setText(0, "Console");
+   item->setText(1, "0");
+   QBrush redBrush(Qt::red);
+   item->setForeground(0, redBrush);
+   item = new QTreeWidgetItem(topItem);
+   item->setText(0, "brestore");
+   item->setText(1, "1");
+   treeWidget->expandItem(topItem);
+
+   stackedWidget->setCurrentIndex(0);
+}
+
+/*
+ * Handle up and down arrow keys for the command line
+ *  history.
+ */
+void MainWin::keyPressEvent(QKeyEvent *event)
+{
+   if (m_cmd_history.size() == 0) {
+      event->ignore();
+      return;
+   }
+   switch (event->key()) {
+   case Qt::Key_Down:
+      if (m_cmd_last < 0 || m_cmd_last >= (m_cmd_history.size()-1)) {
+         event->ignore();
+         return;
+      }
+      m_cmd_last++;
+      break;
+   case Qt::Key_Up:
+      if (m_cmd_last == 0) {
+         event->ignore();
+         return;
+      }
+      if (m_cmd_last < 0 || m_cmd_last > (m_cmd_history.size()-1)) {
+         m_cmd_last = m_cmd_history.size() - 1;
+      } else {
+         m_cmd_last--;
+      }
+      break;
+   default:
+      event->ignore();
+      return;
+   }
+   lineEdit->setText(m_cmd_history[m_cmd_last]);
+}
 
+void MainWin::createConnections()
+{
    /* Connect signals to slots */
    connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(input_line()));
    connect(actionAbout_bat, SIGNAL(triggered()), this, SLOT(about()));
@@ -58,20 +140,86 @@ MainWin::MainWin(QWidget *parent) : QMainWindow(parent)
            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
    connect(treeWidget, SIGNAL(itemPressed(QTreeWidgetItem *, int)), this, 
            SLOT(treeItemClicked(QTreeWidgetItem *, int)));
+   connect(treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, 
+           SLOT(treeItemDoubleClicked(QTreeWidgetItem *, int)));
+
    connect(actionQuit, SIGNAL(triggered()), app, SLOT(closeAllWindows()));
    connect(actionConnect, SIGNAL(triggered()), m_console, SLOT(connect()));
+   connect(actionStatusDir, SIGNAL(triggered()), m_console, SLOT(status_dir()));
+   connect(actionSelectFont, SIGNAL(triggered()), m_console, SLOT(set_font()));
+   connect(actionLabel, SIGNAL(triggered()), this,  SLOT(labelDialogClicked()));
+   connect(actionRun, SIGNAL(triggered()), this,  SLOT(runDialogClicked()));
+   connect(actionRestore, SIGNAL(triggered()), this,  SLOT(restoreDialogClicked()));
+}
+
+/* 
+ * Reimplementation of QWidget closeEvent virtual function   
+ */
+void MainWin::closeEvent(QCloseEvent *event)
+{
+   writeSettings();
+   m_console->writeSettings();
+   m_console->terminate();
+   event->accept();
+}
+
+void MainWin::writeSettings()
+{
+   QSettings settings("bacula.org", "bat");
+
+   settings.beginGroup("MainWin");
+   settings.setValue("winSize", size());
+   settings.setValue("winPos", pos());
+   settings.endGroup();
+}
+
+void MainWin::readSettings()
+{ 
+   QSettings settings("bacula.org", "bat");
 
+   settings.beginGroup("MainWin");
+   resize(settings.value("winSize", QSize(1041, 801)).toSize());
+   move(settings.value("winPos", QPoint(200, 150)).toPoint());
+   settings.endGroup();
 }
 
 void MainWin::treeItemClicked(QTreeWidgetItem *item, int column)
 {
    (void)column;
    int index = item->text(1).toInt();
-   if (index >= 0 && index < 2) {
+   if (index >= 0 && index < 4) {
       stackedWidget->setCurrentIndex(index);
    }
 }
 
+/*
+ */
+void MainWin::treeItemDoubleClicked(QTreeWidgetItem *item, int column)
+{
+   (void)column;
+   int index = item->text(1).toInt();
+   /* ***FIXME**** make this automatic */
+   if (index >= 0 && index < 4) {
+      stackedWidget->setCurrentIndex(index);
+   }
+}
+
+void MainWin::labelDialogClicked() 
+{
+   new labelDialog(m_console);
+}
+
+void MainWin::runDialogClicked() 
+{
+   new runDialog(m_console);
+}
+
+void MainWin::restoreDialogClicked() 
+{
+   new prerestoreDialog(m_console);
+}
+
+
 
 /*
  * The user just finished typing a line in the command line edit box
@@ -80,9 +228,17 @@ void MainWin::input_line()
 {
    QString cmdStr = lineEdit->text();    /* Get the text */
    lineEdit->clear();                    /* clear the lineEdit box */
-   textEdit->append(cmdStr);             /* append text on screen */
-   m_console->write_dir(cmdStr.toUtf8().data());         /* send to dir */
+   if (m_console->is_connected()) {
+      m_console->display_text(cmdStr + "\n");
+      m_console->write_dir(cmdStr.toUtf8().data());         /* send to dir */
+   } else {
+      set_status("Director not connected. Click on connect button.");
+   }
+   m_cmd_history.append(cmdStr);
+   m_cmd_last = -1;
 }
+
+
 void MainWin::about()
 {
    QMessageBox::about(this, tr("About bat"),
@@ -92,23 +248,7 @@ void MainWin::about()
                " interface to the Director."));
 }
 
-void set_textf(const char *fmt, ...)
-{
-   va_list arg_ptr;
-   char buf[1000];
-   int len;
-   va_start(arg_ptr, fmt);
-   len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
-   va_end(arg_ptr);
-   mainWin->textEdit->append(buf);
-}
-
-void set_text(const char *buf)
-{
-   mainWin->textEdit->append(buf);
-}
-
-void set_statusf(const char *fmt, ...)
+void MainWin::set_statusf(const char *fmt, ...)
 {
    va_list arg_ptr;
    char buf[1000];
@@ -116,20 +256,15 @@ void set_statusf(const char *fmt, ...)
    va_start(arg_ptr, fmt);
    len = bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
    va_end(arg_ptr);
-// gtk_label_set_text(GTK_LABEL(status1), buf);
-// set_scroll_bar_to_end();
-// ready = false;
+   set_status(buf);
 }
 
-void set_status_ready()
+void MainWin::set_status_ready()
 {
-   mainWin->statusBar()->showMessage("Ready");
-// ready = true;
-// set_scroll_bar_to_end();
+   set_status(" Ready");
 }
 
-void set_status(const char *buf)
+void MainWin::set_status(const char *buf)
 {
-   mainWin->statusBar()->showMessage(buf);
-// ready = false;
+   statusBar()->showMessage(buf);
 }