1 /* 2 * Copyright 2016 CodiLime 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 #pragma once 18 19 #include <QFileDialog> 20 #include <QIODevice> 21 #include <QList> 22 #include <QMainWindow> 23 #include <QMutex> 24 #include <QTextStream> 25 26 #include "ui_logwidget.h" 27 28 namespace veles { 29 namespace ui { 30 31 /*****************************************************************************/ 32 /* IODeviceProxy */ 33 /*****************************************************************************/ 34 35 class IODeviceProxy : public QIODevice { 36 Q_OBJECT 37 public: 38 QMutex* mutexHistory(); 39 QList<QString>& history(); 40 41 protected: 42 qint64 readData(char* data, qint64 maxSize) Q_DECL_OVERRIDE; 43 qint64 writeData(const char* data, qint64 maxSize) Q_DECL_OVERRIDE; 44 45 signals: 46 void newString(QString str); 47 48 private: 49 QList<QString> history_; 50 static constexpr int max_history_size_ = 100; 51 52 // Controls access to the history_. 53 QMutex mutex_; 54 }; 55 56 /*****************************************************************************/ 57 /* LogWidget */ 58 /*****************************************************************************/ 59 60 // Thread-safe usage: 61 // QTextStream out(veles::ui::LogWidget::output()); 62 // out << "Log message." << endl; 63 // 64 // LogWidget::output() always returns non-null pointer and it's value is not 65 // going to change through application's lifetime (QTextStream using 66 // output()'s value can be created once and safely kept). 67 68 class LogWidget : public QMainWindow { 69 Q_OBJECT 70 71 public: 72 explicit LogWidget(QWidget* parent = nullptr); 73 ~LogWidget() override; 74 static QIODevice* output(); 75 76 public slots: 77 void clearLog(); 78 void append(QString text); 79 void saveFileSelected(const QString& file); 80 81 private: 82 void appendHistory(); 83 void setupSaveFileDialog(); 84 static void checkIODevice(); 85 86 Ui::LogWidget* ui_; 87 QFileDialog* file_dialog_; 88 89 static IODeviceProxy* io_proxy_; 90 }; 91 92 } // namespace ui 93 } // namespace veles 94