From ba90513fe7516d2736fd1462ebe00042768f27a6 Mon Sep 17 00:00:00 2001 From: lupoDharkael Date: Tue, 11 Jul 2017 17:46:22 +0200 Subject: [PATCH] Add base code for custom filenames --- docs/dev/qsettings.md | 4 ++ flameshot.pro | 10 ++- src/capture/screenshot.cpp | 8 +-- src/config/configwindow.cpp | 11 +++- src/config/filenameeditor.cpp | 64 +++++++++++++++++++ src/config/filenameeditor.h | 50 +++++++++++++++ src/config/strftimechooserwidget.cpp | 93 ++++++++++++++++++++++++++++ src/config/strftimechooserwidget.h | 20 ++++++ src/utils/filenamehandler.cpp | 63 +++++++++++++++++++ src/utils/filenamehandler.h | 45 ++++++++++++++ 10 files changed, 360 insertions(+), 8 deletions(-) create mode 100644 src/config/filenameeditor.cpp create mode 100644 src/config/filenameeditor.h create mode 100644 src/config/strftimechooserwidget.cpp create mode 100644 src/config/strftimechooserwidget.h create mode 100644 src/utils/filenamehandler.cpp create mode 100644 src/utils/filenamehandler.h diff --git a/docs/dev/qsettings.md b/docs/dev/qsettings.md index d0b44b76..02a5b5ca 100644 --- a/docs/dev/qsettings.md +++ b/docs/dev/qsettings.md @@ -30,3 +30,7 @@ - value: "showDesktopNotification" - type: bool - description: show every desktop notification. +- filename pattern + - value: "filenamePattern" + - type: QString + - description: pattern for the saved files. diff --git a/flameshot.pro b/flameshot.pro index 79830e44..70f15b6a 100644 --- a/flameshot.pro +++ b/flameshot.pro @@ -49,7 +49,10 @@ SOURCES += src/main.cpp\ src/config/uicoloreditor.cpp \ src/config/geneneralconf.cpp \ src/flameshotdbusadapter.cpp \ - src/config/clickablelabel.cpp + src/config/clickablelabel.cpp \ + src/config/filenameeditor.cpp \ + src/utils/filenamehandler.cpp \ + src/config/strftimechooserwidget.cpp HEADERS += \ src/controller.h \ @@ -65,7 +68,10 @@ HEADERS += \ src/config/uicoloreditor.h \ src/config/geneneralconf.h \ src/flameshotdbusadapter.h \ - src/config/clickablelabel.h + src/config/clickablelabel.h \ + src/config/filenameeditor.h \ + src/utils/filenamehandler.h \ + src/config/strftimechooserwidget.h RESOURCES += \ graphics.qrc diff --git a/src/capture/screenshot.cpp b/src/capture/screenshot.cpp index 42dba7c6..d34b5f3a 100644 --- a/src/capture/screenshot.cpp +++ b/src/capture/screenshot.cpp @@ -18,10 +18,10 @@ #include "screenshot.h" #include "button.h" #include "capturemodification.h" +#include "src/utils/filenamehandler.h" #include #include #include -#include #include #include #include @@ -72,8 +72,9 @@ QString Screenshot::graphicalSave(const QRect &selection, QWidget *parent) const savePath = QDir::currentPath(); } } + + QString tempName = "/"+ FileNameHandler().getParsedPattern(); // find unused name adding _n where n is a number - QString tempName = QObject::tr("/screenshot"); QFileInfo checkFile(savePath + tempName + "." + format); if (checkFile.exists()) { tempName += "_"; @@ -291,8 +292,7 @@ void Screenshot::uploadToImgur(QNetworkAccessManager *accessManager, const QRect &selection) { QString title ="flameshot_screenshot"; - QString datetime = QDateTime().toString(); - QString description = "flameshot " + datetime; + QString description = FileNameHandler().getParsedPattern(); QPixmap pixToSave; if (selection.isEmpty()) { pixToSave = m_modifiedScreenshot; diff --git a/src/config/configwindow.cpp b/src/config/configwindow.cpp index 37f87c74..30d52e98 100644 --- a/src/config/configwindow.cpp +++ b/src/config/configwindow.cpp @@ -20,18 +20,18 @@ #include "src/config/buttonlistview.h" #include "src/config/uicoloreditor.h" #include "src/config/geneneralconf.h" +#include "src/config/filenameeditor.h" #include #include #include #include #include -#include // ConfigWindow contains the menus where you can configure the application ConfigWindow::ConfigWindow(QWidget *parent) : QWidget(parent) { setAttribute(Qt::WA_DeleteOnClose); - setFixedSize(400, 450); + setFixedSize(410, 540); setWindowIcon(QIcon(":img/flameshot.png")); setWindowTitle(tr("Configuration")); @@ -57,6 +57,13 @@ ConfigWindow::ConfigWindow(QWidget *parent) : QWidget(parent) { m_buttonListView->setFlow(QListWidget::TopToBottom); m_layout->addWidget(m_buttonListView); + // name editor + QLabel *nameEditLabel = new QLabel(tr("Filename editor"), this); + m_layout->addWidget(nameEditLabel); + + FileNameEditor *nameEditor = new FileNameEditor(this); + m_layout->addWidget(nameEditor); + show(); } diff --git a/src/config/filenameeditor.cpp b/src/config/filenameeditor.cpp new file mode 100644 index 00000000..7daecb38 --- /dev/null +++ b/src/config/filenameeditor.cpp @@ -0,0 +1,64 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#include "filenameeditor.h" +#include "src/utils/filenamehandler.h" +#include +#include +#include +#include + +FileNameEditor::FileNameEditor(QWidget *parent) : QFrame(parent) { + setFrameStyle(QFrame::StyledPanel); + initWidgets(); + initLayout(); +} + +void FileNameEditor::initLayout() { + m_layout = new QGridLayout(this); + m_layout->addWidget(m_nameEditor, 0,1); + m_layout->addWidget(m_saveButton, 0, 0); + m_layout->addWidget(new QLabel("Preview: ", this), 1, 0); + m_layout->addWidget(m_outputLabel, 1, 1); +} + +void FileNameEditor::initWidgets() { + m_nameHandler = new FileNameHandler(this); + + m_nameEditor = new QLineEdit(this); + m_nameEditor->setMaxLength(FileNameHandler::MAX_CHARACTERS); + + m_outputLabel = new QLabel(this); + m_saveButton = new QPushButton(tr("Save"), this); + + connect(m_nameEditor, &QLineEdit::textChanged, this, + &FileNameEditor::showParsedPattern); + m_nameEditor->setText(m_nameHandler->getActualPattern()); + m_outputLabel->setText(m_nameHandler->getParsedPattern()); + + connect(m_saveButton, &QPushButton::clicked, this, &FileNameEditor::savePattern); +} + +void FileNameEditor::savePattern() { + QString pattern = m_nameEditor->text(); + m_nameHandler->savePattern(pattern); +} + +void FileNameEditor::showParsedPattern(const QString &p) { + QString output = m_nameHandler->parseFilename(p); + m_outputLabel->setText(output); +} diff --git a/src/config/filenameeditor.h b/src/config/filenameeditor.h new file mode 100644 index 00000000..2aec4a0a --- /dev/null +++ b/src/config/filenameeditor.h @@ -0,0 +1,50 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#ifndef FILENAMEEDITOR_H +#define FILENAMEEDITOR_H + +#include + +class QGridLayout; +class QLabel; +class QLineEdit; +class FileNameHandler; +class QPushButton; + +class FileNameEditor : public QFrame +{ + Q_OBJECT +public: + explicit FileNameEditor(QWidget *parent = nullptr); + +private: + QGridLayout *m_layout; + QLabel *m_outputLabel; + QLineEdit *m_nameEditor; + QPushButton *m_saveButton; + FileNameHandler *m_nameHandler; + + void initLayout(); + void initWidgets(); + +private slots: + void savePattern(); + void showParsedPattern(const QString &); +}; + +#endif // FILENAMEEDITOR_H diff --git a/src/config/strftimechooserwidget.cpp b/src/config/strftimechooserwidget.cpp new file mode 100644 index 00000000..0641d288 --- /dev/null +++ b/src/config/strftimechooserwidget.cpp @@ -0,0 +1,93 @@ +#include "strftimechooserwidget.h" + +StrftimeChooserWidget::StrftimeChooserWidget(QWidget *parent) : QWidget(parent) { + +} + +//QStringList StrftimeChooserWidget::m_valuesStr = QList() +// << "%a" +// << "%A" +// << "%b" +// << "%B" +// << "%C" +// << "%d" +// << "%D" +// << "%e" +// << "%E" +// << "%F" +// << "%G" +// << "%g" +// << "%h" +// << "%H" +// << "%I" +// << "%j" +// << "%k" +// << "%l" +// << "%m" +// << "%M" +// << "%n" +// << "%O" +// << "%p" +// << "%P" +// << "%r" +// << "%R" +// << "%s" +// << "%S" +// << "%t" +// << "%T" +// << "%u" +// << "%U" +// << "%V" +// << "%w" +// << "%W" +// << "%x" +// << "%X" +// << "%y" +// << "%Y" +// << "%z" +// << "%Z" +// << "%%"; + +//QStringList StrftimeChooserWidget::m_buttonLabel = QList() +// << "Day (Mon)" //"%a" +// << "Day (Monday)" //"%A" +// << "Month (Jan)" //"%b" +// << "Month (1...12)" //"%B" +// << "Century (21)" //"%C" +// << "Day (01...31)" //"%d" +// << "Full Date (%m/%d/%y)" //"%D" +// << " ()" //"%e" +// << " ()" //"%E" +// << " ()" //"%F" +// << " ()" //"%G" +// << " ()" //"%g" +// << " ()" //"%h" +// << " ()" //"%H" +// << " ()" //"%I" +// << " ()" //"%j" +// << " ()" //"%k" +// << " ()" //"%l" +// << " ()" //"%m" +// << " ()" //"%M" +// << " ()" //"%n" +// << " ()" //"%O" +// << " ()" //"%p" +// << " ()" //"%P" +// << " ()" //"%r" +// << " ()" //"%R" +// << " ()" //"%s" +// << " ()" //"%S" +// << " ()" //"%t" +// << " ()" //"%T" +// << " ()" //"%u" +// << " ()" //"%U" +// << " ()" //"%V" +// << " ()" //"%w" +// << " ()" //"%W" +// << " ()" //"%x" +// << " ()" //"%X" +// << " ()" //"%y" +// << " ()" //"%Y" +// << " ()" //"%z" +// << " ()" //"%Z" +// << "%"; //"%%"; diff --git a/src/config/strftimechooserwidget.h b/src/config/strftimechooserwidget.h new file mode 100644 index 00000000..1fccdb0e --- /dev/null +++ b/src/config/strftimechooserwidget.h @@ -0,0 +1,20 @@ +#ifndef STRFTIMECHOOSERWIDGET_H +#define STRFTIMECHOOSERWIDGET_H + +#include + +class StrftimeChooserWidget : public QWidget +{ + Q_OBJECT +public: + explicit StrftimeChooserWidget(QWidget *parent = nullptr); + +signals: + void variableEmitted(const QString &); + +private: + static QStringList m_valuesStr; + static QStringList m_buttonLabel; +}; + +#endif // STRFTIMECHOOSERWIDGET_H diff --git a/src/utils/filenamehandler.cpp b/src/utils/filenamehandler.cpp new file mode 100644 index 00000000..1c05a563 --- /dev/null +++ b/src/utils/filenamehandler.cpp @@ -0,0 +1,63 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#include "filenamehandler.h" +#include +#include +#include + +FileNameHandler::FileNameHandler(QObject *parent) : QObject(parent) { + std::locale::global(std::locale(std::locale("").name())); +} + +QString FileNameHandler::getActualPattern() { + return QSettings().value("filenamePattern").toString(); +} + +QString FileNameHandler::getParsedPattern() { + return parseFilename(getActualPattern()); +} + +QString FileNameHandler::parseFilename(const QString &name) { + QString res; + if (name.isEmpty()) { + res = tr("screenshot"); + } else { + std::time_t t = std::time(NULL); + + char *tempData = QStringTocharArr(name); + char data[MAX_CHARACTERS] = {0}; + std::strftime(data, sizeof(data), + tempData, std::localtime(&t)); + res = QString::fromLocal8Bit(data, strlen(data)); + free(tempData); + } + return res; +} + +void FileNameHandler::savePattern(const QString &pattern) { + QSettings().setValue("filenamePattern", pattern); +} + +QString FileNameHandler::charArrToQString(const char *c) { + return QString::fromLocal8Bit(c, MAX_CHARACTERS); +} + +char * FileNameHandler::QStringTocharArr(const QString &s) { + QByteArray ba = s.toLocal8Bit(); + return const_cast(strdup(ba.constData())); +} diff --git a/src/utils/filenamehandler.h b/src/utils/filenamehandler.h new file mode 100644 index 00000000..ab734ba9 --- /dev/null +++ b/src/utils/filenamehandler.h @@ -0,0 +1,45 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#ifndef FILENAMEHANDLER_H +#define FILENAMEHANDLER_H + +#include + + +class FileNameHandler : public QObject +{ + Q_OBJECT +public: + explicit FileNameHandler(QObject *parent = nullptr); + + QString getActualPattern(); + QString getParsedPattern(); + QString parseFilename(const QString &name); + + static const int MAX_CHARACTERS = 70; + +public slots: + void savePattern(const QString &pattern); + +private: + //using charArr = char[MAX_CHARACTERS]; + inline QString charArrToQString(const char *c); + inline char * QStringTocharArr(const QString &s); +}; + +#endif // FILENAMEHANDLER_H