add "Save to File" option to the context dialog for the log window
This commit is contained in:
@ -12,7 +12,17 @@
|
|||||||
------------------------------------------------------------------------- */
|
------------------------------------------------------------------------- */
|
||||||
|
|
||||||
#include "logwindow.h"
|
#include "logwindow.h"
|
||||||
|
|
||||||
|
#include <QAction>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QFileDialog>
|
||||||
|
#include <QIcon>
|
||||||
|
#include <QMenu>
|
||||||
|
#include <QMessageBox>
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
#include <QString>
|
||||||
|
#include <QTextStream>
|
||||||
|
|
||||||
LogWindow::LogWindow(QWidget *parent) : QPlainTextEdit(parent)
|
LogWindow::LogWindow(QWidget *parent) : QPlainTextEdit(parent)
|
||||||
{
|
{
|
||||||
@ -30,6 +40,38 @@ void LogWindow::closeEvent(QCloseEvent *event)
|
|||||||
QPlainTextEdit::closeEvent(event);
|
QPlainTextEdit::closeEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LogWindow::save_as()
|
||||||
|
{
|
||||||
|
QString fileName = QFileDialog::getSaveFileName(this, "Save Log to File");
|
||||||
|
if (fileName.isEmpty()) return;
|
||||||
|
|
||||||
|
QFileInfo path(fileName);
|
||||||
|
QFile file(path.absoluteFilePath());
|
||||||
|
|
||||||
|
if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
|
||||||
|
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QTextStream out(&file);
|
||||||
|
QString text = toPlainText();
|
||||||
|
out << text;
|
||||||
|
if (text.back().toLatin1() != '\n') out << "\n"; // add final newline if missing
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LogWindow::contextMenuEvent(QContextMenuEvent *event)
|
||||||
|
{
|
||||||
|
// show augmented context menu
|
||||||
|
auto *menu = createStandardContextMenu();
|
||||||
|
menu->addSeparator();
|
||||||
|
auto action = menu->addAction(QString("Save Log to File ..."));
|
||||||
|
action->setIcon(QIcon(":/icons/document-save-as.png"));
|
||||||
|
connect(action, &QAction::triggered, this, &LogWindow::save_as);
|
||||||
|
menu->exec(event->globalPos());
|
||||||
|
delete menu;
|
||||||
|
}
|
||||||
|
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
// c-basic-offset: 4
|
// c-basic-offset: 4
|
||||||
// End:
|
// End:
|
||||||
|
|||||||
@ -22,8 +22,12 @@ class LogWindow : public QPlainTextEdit {
|
|||||||
public:
|
public:
|
||||||
LogWindow(QWidget *parent = nullptr);
|
LogWindow(QWidget *parent = nullptr);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void save_as();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void closeEvent(QCloseEvent *event) override;
|
void closeEvent(QCloseEvent *event) override;
|
||||||
|
void contextMenuEvent(QContextMenuEvent *event) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user