1 /*=========================================================================
2 
3   Library:   CTK
4 
5   Copyright (c) Kitware Inc.
6 
7   Licensed under the Apache License, Version 2.0 (the "License");
8   you may not use this file except in compliance with the License.
9   You may obtain a copy of the License at
10 
11       http://www.apache.org/licenses/LICENSE-2.0.txt
12 
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18 
19 =========================================================================*/
20 
21 #ifndef __ctkErrorLogModel_h
22 #define __ctkErrorLogModel_h
23 
24 // Qt includes
25 #include <QSortFilterProxyModel>
26 
27 // CTK includes
28 #include "ctkWidgetsExport.h"
29 #include "ctkErrorLogLevel.h"
30 #include "ctkErrorLogTerminalOutput.h"
31 
32 //------------------------------------------------------------------------------
33 class ctkErrorLogAbstractMessageHandler;
34 class ctkErrorLogModelPrivate;
35 struct ctkErrorLogContext;
36 
37 //------------------------------------------------------------------------------
38 /// \ingroup Widgets
39 class CTK_WIDGETS_EXPORT ctkErrorLogModel : public QSortFilterProxyModel
40 {
41   Q_OBJECT
42   Q_PROPERTY(bool logEntryGrouping READ logEntryGrouping WRITE setLogEntryGrouping)
43   Q_PROPERTY(ctkErrorLogTerminalOutput::TerminalOutputs terminalOutputs READ terminalOutputs WRITE setTerminalOutputs)
44   Q_PROPERTY(bool asynchronousLogging READ asynchronousLogging WRITE  setAsynchronousLogging)
45   Q_PROPERTY(QString filePath READ filePath WRITE  setFilePath)
46   Q_PROPERTY(int numberOfFilesToKeep READ numberOfFilesToKeep WRITE  setNumberOfFilesToKeep)
47   Q_PROPERTY(bool fileLoggingEnabled READ fileLoggingEnabled WRITE  setFileLoggingEnabled)
48   Q_PROPERTY(QString fileLoggingPattern READ fileLoggingPattern WRITE setFileLoggingPattern)
49 public:
50   typedef QSortFilterProxyModel Superclass;
51   typedef ctkErrorLogModel Self;
52   explicit ctkErrorLogModel(QObject* parentObject = 0);
53   virtual ~ctkErrorLogModel();
54 
55   enum ColumnsIds
56     {
57     TimeColumn = 0,
58     ThreadIdColumn,
59     LogLevelColumn,
60     OriginColumn,
61     DescriptionColumn,
62     MaxColumn = DescriptionColumn
63     };
64 
65   enum ItemDataRole{
66     DescriptionTextRole = Qt::UserRole + 1
67     };
68 
69   /// Register a message handler.
70   bool registerMsgHandler(ctkErrorLogAbstractMessageHandler * msgHandler);
71 
72   QStringList msgHandlerNames()const;
73 
74   /// Return True if the handler identified by \a handlerName is enabled
75   bool msgHandlerEnabled(const QString& handlerName) const;
76 
77   /// Enable a specific handler given its name
78   void setMsgHandlerEnabled(const QString& handlerName, bool enabled);
79 
80   /// Return names of the enabled message handlers
81   QStringList msgHandlerEnabled()const;
82 
83   /// Enable handler identified by their names
84   void setMsgHandlerEnabled(const QStringList& handlerNames);
85 
86   void enableAllMsgHandler();
87   void disableAllMsgHandler();
88   void setAllMsgHandlerEnabled(bool enabled);
89 
90   /// Return if messages are both printed into the terminal and added to ctkErrorLogModel.
91   /// \note If TerminalOutput::None is returned, message will only be added to the model.
92   ctkErrorLogTerminalOutput::TerminalOutputs terminalOutputs()const;
93 
94   /// Set terminal output mode
95   /// \sa terminalOutputs()
96   /// \sa TerminalOutput
97   void setTerminalOutputs(const ctkErrorLogTerminalOutput::TerminalOutputs& terminalOutput);
98 
99   ctkErrorLogLevel::LogLevels logLevelFilter()const;
100 
101   void filterEntry(const ctkErrorLogLevel::LogLevels& logLevel = ctkErrorLogLevel::Unknown, bool disableFilter = false);
102 
103   bool logEntryGrouping()const;
104   void setLogEntryGrouping(bool value);
105 
106   bool asynchronousLogging()const;
107   void setAsynchronousLogging(bool value);
108 
109   QString filePath()const;
110   void setFilePath(const QString& filePath);
111 
112   int numberOfFilesToKeep()const;
113   void setNumberOfFilesToKeep(int value);
114 
115   bool fileLoggingEnabled()const;
116   void setFileLoggingEnabled(bool value);
117 
118   QString fileLoggingPattern()const;
119   void setFileLoggingPattern(const QString& value);
120 
121   /// Return log entry information associated with \a row and \a column.
122   /// \internal
123   QVariant logEntryData(int row,
124                         int column = ctkErrorLogModel::DescriptionColumn,
125                         int role = Qt::DisplayRole) const;
126 
127   /// Return log entry information associated with Description column.
128   /// \sa ctkErrorLogModel::DescriptionColumn, logEntryData()
129   Q_INVOKABLE QString logEntryDescription(int row) const;
130 
131   /// Return current number of log entries.
132   /// \sa clear()
133   Q_INVOKABLE int logEntryCount() const;
134 
135 public Q_SLOTS:
136 
137   /// Remove all log entries from model
138   void clear();
139 
140   /// \sa logEntryGrouping(), asynchronousLogging()
141   void addEntry(const QDateTime& currentDateTime, const QString& threadId,
142                 ctkErrorLogLevel::LogLevel logLevel, const QString& origin,
143                 const ctkErrorLogContext &context, const QString& text);
144 
145 Q_SIGNALS:
146   void logLevelFilterChanged();
147 
148   /// \sa addEntry()
149   void entryAdded(ctkErrorLogLevel::LogLevel logLevel);
150 
151 protected:
152   QScopedPointer<ctkErrorLogModelPrivate> d_ptr;
153 
154 private:
155   Q_DECLARE_PRIVATE(ctkErrorLogModel)
156   Q_DISABLE_COPY(ctkErrorLogModel)
157 };
158 
159 #endif
160