Skip to content

Commit

Permalink
Merge pull request #1139 from frostasm/add-support-for-working-with-m…
Browse files Browse the repository at this point in the history
…ultiple-attachments

Add support for working with multiple entry attachments at once
  • Loading branch information
TheZ3ro authored Nov 12, 2017
2 parents f141325 + f34b090 commit b28333e
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 66 deletions.
30 changes: 29 additions & 1 deletion src/core/EntryAttachments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include "EntryAttachments.h"

#include <QStringList>

EntryAttachments::EntryAttachments(QObject* parent)
: QObject(parent)
{
Expand Down Expand Up @@ -71,7 +73,8 @@ void EntryAttachments::set(const QString& key, const QByteArray& value)
void EntryAttachments::remove(const QString& key)
{
if (!m_attachments.contains(key)) {
Q_ASSERT(false);
Q_ASSERT_X(false, "EntryAttachments::remove",
qPrintable(QString("Can't find attachment for key %1").arg(key)));
return;
}

Expand All @@ -83,6 +86,31 @@ void EntryAttachments::remove(const QString& key)
emit modified();
}

void EntryAttachments::remove(const QStringList &keys)
{
if (keys.isEmpty()) {
return;
}

bool isModified = false;
for (const QString &key: keys) {
if (!m_attachments.contains(key)) {
Q_ASSERT_X(false, "EntryAttachments::remove",
qPrintable(QString("Can't find attachment for key %1").arg(key)));
continue;
}

isModified = true;
emit aboutToBeRemoved(key);
m_attachments.remove(key);
emit removed(key);
}

if (isModified) {
emit modified();
}
}

void EntryAttachments::clear()
{
if (m_attachments.isEmpty()) {
Expand Down
3 changes: 3 additions & 0 deletions src/core/EntryAttachments.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <QMap>
#include <QObject>

class QStringList;

class EntryAttachments : public QObject
{
Q_OBJECT
Expand All @@ -33,6 +35,7 @@ class EntryAttachments : public QObject
QByteArray value(const QString& key) const;
void set(const QString& key, const QByteArray& value);
void remove(const QString& key);
void remove(const QStringList& keys);
void clear();
void copyDataFrom(const EntryAttachments* other);
bool operator==(const EntryAttachments& other) const;
Expand Down
72 changes: 70 additions & 2 deletions src/gui/FileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ QString FileDialog::getOpenFileName(QWidget* parent, const QString& caption, QSt
{
if (!m_nextFileName.isEmpty()) {
QString result = m_nextFileName;
m_nextFileName = "";
m_nextFileName.clear();
return result;
}
else {
Expand All @@ -51,13 +51,43 @@ QString FileDialog::getOpenFileName(QWidget* parent, const QString& caption, QSt
}
}

QStringList FileDialog::getOpenFileNames(QWidget *parent, const QString &caption, QString dir,
const QString &filter, QString *selectedFilter,
QFileDialog::Options options)
{
if (!m_nextFileNames.isEmpty()) {
QStringList results = m_nextFileNames;
m_nextFileNames.clear();
return results;
}
else {
if (dir.isEmpty()) {
dir = config()->get("LastDir").toString();
}

QStringList results = QFileDialog::getOpenFileNames(parent, caption, dir, filter,
selectedFilter, options);

// on Mac OS X the focus is lost after closing the native dialog
if (parent) {
parent->activateWindow();
}

if (!results.isEmpty()) {
config()->set("LastDir", QFileInfo(results[0]).absolutePath());
}

return results;
}
}

QString FileDialog::getSaveFileName(QWidget* parent, const QString& caption, QString dir,
const QString& filter, QString* selectedFilter,
QFileDialog::Options options, const QString& defaultExtension)
{
if (!m_nextFileName.isEmpty()) {
QString result = m_nextFileName;
m_nextFileName = "";
m_nextFileName.clear();
return result;
}
else {
Expand Down Expand Up @@ -103,11 +133,49 @@ QString FileDialog::getSaveFileName(QWidget* parent, const QString& caption, QSt
}
}

QString FileDialog::getExistingDirectory(QWidget *parent, const QString &caption, QString dir,
QFileDialog::Options options)
{
if (!m_nextDirName.isEmpty()) {
QString result = m_nextDirName;
m_nextDirName.clear();
return result;
}
else {
if (dir.isEmpty()) {
dir = config()->get("LastDir").toString();
}

dir = QFileDialog::getExistingDirectory(parent, caption, dir, options);

// on Mac OS X the focus is lost after closing the native dialog
if (parent) {
parent->activateWindow();
}

if (!dir.isEmpty()) {
config()->set("LastDir", QFileInfo(dir).absolutePath());
}

return dir;
}
}

void FileDialog::setNextFileName(const QString& fileName)
{
m_nextFileName = fileName;
}

void FileDialog::setNextFileNames(const QStringList &fileNames)
{
m_nextFileNames = fileNames;
}

void FileDialog::setNextDirName(const QString &dirName)
{
m_nextDirName = dirName;
}

FileDialog::FileDialog()
{
}
Expand Down
9 changes: 9 additions & 0 deletions src/gui/FileDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,31 @@ class FileDialog
QString getOpenFileName(QWidget* parent = nullptr, const QString& caption = QString(),
QString dir = QString(), const QString& filter = QString(),
QString* selectedFilter = nullptr, QFileDialog::Options options = 0);
QStringList getOpenFileNames(QWidget* parent = nullptr, const QString& caption = QString(),
QString dir = QString(), const QString& filter = QString(),
QString* selectedFilter = nullptr, QFileDialog::Options options = 0);
QString getSaveFileName(QWidget* parent = nullptr, const QString& caption = QString(),
QString dir = QString(), const QString& filter = QString(),
QString* selectedFilter = nullptr, QFileDialog::Options options = 0,
const QString& defaultExtension = QString());
QString getExistingDirectory(QWidget* parent = nullptr, const QString& caption = QString(),
QString dir = QString(), QFileDialog::Options options = QFileDialog::ShowDirsOnly);

/**
* Sets the result of the next get* method call.
* Use only for testing.
*/
void setNextFileName(const QString& fileName);
void setNextFileNames(const QStringList& fileNames);
void setNextDirName(const QString& dirName);

static FileDialog* instance();

private:
FileDialog();
QString m_nextFileName;
QStringList m_nextFileNames;
QString m_nextDirName;

static FileDialog* m_instance;

Expand Down
Loading

0 comments on commit b28333e

Please sign in to comment.