Pārlūkot izejas kodu

Add ctkErrorLogStatusMessageHandler

If installed, this handler allow to catch the status message displayed
in the QMainWindow statusBar.
Jean-Christophe Fillion-Robin 14 gadi atpakaļ
vecāks
revīzija
5a7e1612a3

+ 3 - 0
Libs/Widgets/CMakeLists.txt

@@ -48,6 +48,8 @@ SET(KIT_SRCS
   ctkDoubleSlider.h
   ctkDynamicSpacer.cpp
   ctkDynamicSpacer.h
+  ctkErrorLogStatusMessageHandler.cpp
+  ctkErrorLogStatusMessageHandler.h
   ctkErrorLogWidget.cpp
   ctkErrorLogWidget.h
   ctkFileDialog.cpp
@@ -167,6 +169,7 @@ SET(KIT_MOC_SRCS
   ctkDoubleSlider.h
   ctkDynamicSpacer.h
   ctkErrorLogWidget.h
+  ctkErrorLogStatusMessageHandler.h
   ctkFileDialog.h
   ctkFittedTextBrowser.h
   ctkFlowLayout.h

+ 90 - 0
Libs/Widgets/ctkErrorLogStatusMessageHandler.cpp

@@ -0,0 +1,90 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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 <QDebug>
+#include <QMainWindow>
+#include <QStatusBar>
+
+// CTK includes
+#include "ctkErrorLogStatusMessageHandler.h"
+
+// --------------------------------------------------------------------------
+QString ctkErrorLogStatusMessageHandler::HandlerName = QLatin1String("Status");
+
+// --------------------------------------------------------------------------
+ctkErrorLogStatusMessageHandler::ctkErrorLogStatusMessageHandler() :
+  QObject(), ctkErrorLogAbstractMessageHandler()
+{
+}
+
+// --------------------------------------------------------------------------
+QString ctkErrorLogStatusMessageHandler::handlerName()const
+{
+  return ctkErrorLogStatusMessageHandler::HandlerName;
+}
+
+// --------------------------------------------------------------------------
+void ctkErrorLogStatusMessageHandler::setEnabledInternal(bool value)
+{
+  QMainWindow * mainWindow = 0;
+  foreach(QWidget* widget, qApp->topLevelWidgets())
+    {
+    mainWindow = qobject_cast<QMainWindow*>(widget);
+    if (mainWindow)
+      {
+      break;
+      }
+    }
+  if (!mainWindow)
+    {
+    qCritical() << "ctkErrorLogStatusMessageHandler - "
+                   " QMainWindow object should be instantiated before enabling ctkErrorLogStatusMessageHandler";
+    return;
+    }
+  if (!mainWindow->statusBar())
+    {
+    qCritical() << "ctkErrorLogStatusMessageHandler - Failed to enable the message handler: "
+                   "There is no StatusBar associated with QMainWindow" << mainWindow;
+    return;
+    }
+  if (value)
+    {
+    connect(mainWindow->statusBar(), SIGNAL(messageChanged(QString)),
+            this, SLOT(statusBarMessageChanged(QString)));
+    }
+  else
+    {
+    disconnect(mainWindow->statusBar(), SIGNAL(messageChanged(QString)),
+               this, SLOT(statusBarMessageChanged(QString)));
+    }
+}
+
+//------------------------------------------------------------------------------
+void ctkErrorLogStatusMessageHandler::statusBarMessageChanged(const QString& text)
+{
+  if (text.isEmpty())
+    {
+    return;
+    }
+  this->errorLogModel()->addEntry(
+        ctkErrorLogModel::Status, this->handlerPrettyName(), text.toLatin1());
+}

+ 46 - 0
Libs/Widgets/ctkErrorLogStatusMessageHandler.h

@@ -0,0 +1,46 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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.
+
+=========================================================================*/
+
+#ifndef __ctkErrorLogStatusMessageHandler_h
+#define __ctkErrorLogStatusMessageHandler_h
+
+// CTK includes
+#include <ctkErrorLogModel.h>
+#include "ctkWidgetsExport.h"
+
+//------------------------------------------------------------------------------
+class CTK_WIDGETS_EXPORT ctkErrorLogStatusMessageHandler :
+    public QObject, public ctkErrorLogAbstractMessageHandler
+{
+  Q_OBJECT
+public:
+  ctkErrorLogStatusMessageHandler();
+
+  static QString HandlerName;
+
+  virtual QString handlerName()const;
+  virtual void setEnabledInternal(bool value);
+
+public slots:
+  void statusBarMessageChanged(const QString& text);
+};
+
+#endif
+