Преглед на файлове

Merge remote-tracking branch 'finetjul/315-ctkconsole-open-file'

* finetjul/315-ctkconsole-open-file:
  Add open file dialog and print help message actions

Conflicts:
	Libs/Widgets/Testing/Cpp/CMakeLists.txt
	Libs/Widgets/ctkConsole.cpp
Jean-Christophe Fillion-Robin преди 11 години
родител
ревизия
fba83ac338
променени са 4 файла, в които са добавени 136 реда и са изтрити 0 реда
  1. 3 0
      Libs/Widgets/Testing/Cpp/CMakeLists.txt
  2. 72 0
      Libs/Widgets/Testing/Cpp/ctkConsoleTest.cpp
  3. 48 0
      Libs/Widgets/ctkConsole.cpp
  4. 13 0
      Libs/Widgets/ctkConsole.h

+ 3 - 0
Libs/Widgets/Testing/Cpp/CMakeLists.txt

@@ -21,6 +21,7 @@ set(TEST_SOURCES
   ctkColorPickerButtonTest1.cpp
   ctkComboBoxTest1.cpp
   ctkCompleterTest1.cpp
+  ctkConsoleTest.cpp
   ctkConsoleTest1.cpp
   ctkCoordinatesWidgetTest.cpp
   ctkCoordinatesWidgetTest1.cpp
@@ -199,6 +200,7 @@ if(CTK_USE_QTTESTING)
 endif()
 
 set(Tests_MOC_CPPS
+  ctkConsoleTest.cpp
   ctkCoordinatesWidgetTest.cpp
   ctkCoordinatesWidgetValueProxyTest.cpp
   ctkDoubleRangeSliderTest.cpp
@@ -274,6 +276,7 @@ SIMPLE_TEST( ctkColorDialogTest2 )
 SIMPLE_TEST( ctkColorPickerButtonTest1 )
 SIMPLE_TEST( ctkComboBoxTest1 )
 SIMPLE_TEST( ctkCompleterTest1 )
+SIMPLE_TEST( ctkConsoleTest )
 SIMPLE_TEST( ctkConsoleTest1 )
 SIMPLE_TEST( ctkCoordinatesWidgetTest )
 SIMPLE_TEST( ctkCoordinatesWidgetTest1 )

+ 72 - 0
Libs/Widgets/Testing/Cpp/ctkConsoleTest.cpp

@@ -0,0 +1,72 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QApplication>
+#include <QString>
+#include <QTimer>
+
+// CTK includes
+#include "ctkConfig.h"
+#include "ctkConsole.h"
+#include "ctkTest.h"
+
+// ----------------------------------------------------------------------------
+class ctkConsoleTester: public QObject
+{
+  Q_OBJECT
+private slots:
+  void testShow();
+
+  void testRunFile();
+  void testRunFile_data();
+};
+
+// ----------------------------------------------------------------------------
+void ctkConsoleTester::testShow()
+{
+  ctkConsole console;
+  console.show();
+  QTest::qWaitForWindowShown(&console);
+}
+
+// ----------------------------------------------------------------------------
+void ctkConsoleTester::testRunFile()
+{
+  ctkConsole console;
+  QFETCH(QString, file);
+  console.runFile(file);
+}
+
+// ----------------------------------------------------------------------------
+void ctkConsoleTester::testRunFile_data()
+{
+  QTest::addColumn<QString>("file");
+  QTest::newRow("null") << QString();
+  QTest::newRow("empty") << "";
+  QTest::newRow("invalid") << " ";
+  QTest::newRow(".") << ".";
+  QTest::newRow("file")
+    << QFileInfo(QDir(CTK_SOURCE_DIR), "README").absoluteFilePath();
+}
+
+// ----------------------------------------------------------------------------
+CTK_TEST_MAIN(ctkConsoleTest)
+#include "moc_ctkConsoleTest.cpp"

+ 48 - 0
Libs/Widgets/ctkConsole.cpp

@@ -50,9 +50,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 // Qt includes
 #include <QAbstractItemView>
+#include <QAction>
 #include <QApplication>
 #include <QClipboard>
 #include <QCompleter>
+#include <QFile>
+#include <QFileDialog>
 #include <QKeyEvent>
 #include <QMimeData>
 #include <QPointer>
@@ -125,6 +128,16 @@ void ctkConsolePrivate::init()
   this->CommandHistory.append("");
   this->CommandPosition = 0;
 
+  QAction* runFileAction = new QAction(q->tr("&Run file"),q);
+  runFileAction->setShortcut(q->tr("Ctrl+r"));
+  connect(runFileAction, SIGNAL(triggered()), q, SLOT(runFile()));
+  q->addAction(runFileAction);
+
+  QAction* printHelpAction = new QAction(q->tr("Print &help"),q);
+  printHelpAction->setShortcut(q->tr("Ctrl+h"));
+  connect(printHelpAction, SIGNAL(triggered()), q, SLOT(printHelp()));
+  q->addAction(printHelpAction);
+
   QVBoxLayout * layout = new QVBoxLayout(q);
   layout->setMargin(0);
   layout->addWidget(this);
@@ -817,6 +830,41 @@ void ctkConsole::exec(const QString& command)
 }
 
 //-----------------------------------------------------------------------------
+void ctkConsole::runFile(const QString& filePath)
+{
+  QFile file(filePath);
+  if (!file.open(QIODevice::ReadOnly))
+    {
+    qWarning() << tr( "File '%1' can't be read.").arg(filePath);
+    return;
+    }
+  for (QTextStream fileStream(&file); !fileStream.atEnd();)
+    {
+    QString line = fileStream.readLine();
+    this->exec(line);
+    }
+}
+
+//-----------------------------------------------------------------------------
+void ctkConsole::runFile()
+{
+  QString filePath =
+    QFileDialog::getOpenFileName(this, tr("Select a script file to run"));
+  if (!filePath.isEmpty())
+    {
+    this->runFile(filePath);
+    }
+}
+
+//-----------------------------------------------------------------------------
+void ctkConsole::printHelp()
+{
+  this->printMessage("\n", Qt::gray);
+  this->printMessage(tr("CTRL+h: Print this help message\n"), Qt::gray);
+  this->printMessage(tr("CTRL+r: Open a file dialog to select a file to run\n"), Qt::gray);
+}
+
+//-----------------------------------------------------------------------------
 void ctkConsole::executeCommand(const QString& command)
 {
   qWarning() << "ctkConsole::executeCommand not implemented !";

+ 13 - 0
Libs/Widgets/ctkConsole.h

@@ -190,8 +190,21 @@ public Q_SLOTS:
   virtual void reset();
 
   /// Exec the contents of the last console line
+  /// \sa openFile(), runFile(QString)
   virtual void exec(const QString&);
 
+  /// Exec line by line the content of a file.
+  /// \sa openFile(), exec()
+  virtual void runFile(const QString& filePath);
+
+  /// Open a file dialog to select a file and run it.
+  /// Shortcut: CTRL+O
+  /// \sa runFile(QString), exec()
+  virtual void runFile();
+
+  /// Print the console help with shortcuts.
+  virtual void printHelp();
+
 protected:
 
   /// Prompt the user for input