2014-06-28 15:09:23 +00:00
|
|
|
#include "ScreenshotsPage.h"
|
|
|
|
#include "ui_ScreenshotsPage.h"
|
|
|
|
|
|
|
|
#include <QModelIndex>
|
|
|
|
#include <QMutableListIterator>
|
2014-07-05 11:26:37 +00:00
|
|
|
#include <QMap>
|
|
|
|
#include <QSet>
|
2014-06-28 15:09:23 +00:00
|
|
|
#include <QFileIconProvider>
|
|
|
|
#include <QFileSystemModel>
|
|
|
|
#include <QStyledItemDelegate>
|
|
|
|
#include <QLineEdit>
|
2014-07-12 21:02:52 +00:00
|
|
|
#include <QEvent>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QKeyEvent>
|
2014-06-28 15:09:23 +00:00
|
|
|
|
2015-03-01 21:20:57 +00:00
|
|
|
#include <MultiMC.h>
|
2014-06-28 15:09:23 +00:00
|
|
|
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "dialogs/ProgressDialog.h"
|
|
|
|
#include "dialogs/CustomMessageBox.h"
|
|
|
|
#include "net/NetJob.h"
|
|
|
|
#include "screenshots/ImgurUpload.h"
|
|
|
|
#include "screenshots/ImgurAlbumCreation.h"
|
|
|
|
#include "tasks/SequentialTask.h"
|
2014-06-28 15:09:23 +00:00
|
|
|
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "RWStorage.h"
|
2015-10-04 23:47:27 +00:00
|
|
|
#include <FileSystem.h>
|
2016-01-05 06:32:52 +00:00
|
|
|
#include <DesktopServices.h>
|
2014-07-12 21:02:52 +00:00
|
|
|
|
2014-07-05 11:26:37 +00:00
|
|
|
typedef RWStorage<QString, QIcon> SharedIconCache;
|
|
|
|
typedef std::shared_ptr<SharedIconCache> SharedIconCachePtr;
|
|
|
|
|
|
|
|
class ThumbnailingResult : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2014-07-12 21:02:52 +00:00
|
|
|
public slots:
|
|
|
|
inline void emitResultsReady(const QString &path) { emit resultsReady(path); }
|
|
|
|
inline void emitResultsFailed(const QString &path) { emit resultsFailed(path); }
|
|
|
|
signals:
|
2014-07-05 11:26:37 +00:00
|
|
|
void resultsReady(const QString &path);
|
|
|
|
void resultsFailed(const QString &path);
|
|
|
|
};
|
|
|
|
|
2014-07-12 21:02:52 +00:00
|
|
|
class ThumbnailRunnable : public QRunnable
|
2014-07-05 11:26:37 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-07-12 21:02:52 +00:00
|
|
|
ThumbnailRunnable(QString path, SharedIconCachePtr cache)
|
2014-07-05 11:26:37 +00:00
|
|
|
{
|
|
|
|
m_path = path;
|
|
|
|
m_cache = cache;
|
|
|
|
}
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
QFileInfo info(m_path);
|
2014-07-12 21:02:52 +00:00
|
|
|
if (info.isDir())
|
2014-07-05 11:26:37 +00:00
|
|
|
return;
|
2014-07-12 21:02:52 +00:00
|
|
|
if ((info.suffix().compare("png", Qt::CaseInsensitive) != 0))
|
2014-07-05 11:26:37 +00:00
|
|
|
return;
|
|
|
|
int tries = 5;
|
2014-07-12 21:02:52 +00:00
|
|
|
while (tries)
|
2014-07-05 11:26:37 +00:00
|
|
|
{
|
2014-07-12 21:02:52 +00:00
|
|
|
if (!m_cache->stale(m_path))
|
2014-07-05 11:26:37 +00:00
|
|
|
return;
|
|
|
|
QImage image(m_path);
|
|
|
|
if (image.isNull())
|
|
|
|
{
|
|
|
|
QThread::msleep(500);
|
|
|
|
tries--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QImage small;
|
2014-07-12 21:02:52 +00:00
|
|
|
if (image.width() > image.height())
|
2014-07-05 11:26:37 +00:00
|
|
|
small = image.scaledToWidth(512).scaledToWidth(256, Qt::SmoothTransformation);
|
|
|
|
else
|
|
|
|
small = image.scaledToHeight(512).scaledToHeight(256, Qt::SmoothTransformation);
|
2014-07-12 21:02:52 +00:00
|
|
|
QPoint offset((256 - small.width()) / 2, (256 - small.height()) / 2);
|
|
|
|
QImage square(QSize(256, 256), QImage::Format_ARGB32);
|
2014-07-05 11:26:37 +00:00
|
|
|
square.fill(Qt::transparent);
|
2014-07-12 21:02:52 +00:00
|
|
|
|
2014-07-05 11:26:37 +00:00
|
|
|
QPainter painter(&square);
|
|
|
|
painter.drawImage(offset, small);
|
|
|
|
painter.end();
|
2014-07-12 21:02:52 +00:00
|
|
|
|
2014-07-05 11:26:37 +00:00
|
|
|
QIcon icon(QPixmap::fromImage(square));
|
|
|
|
m_cache->add(m_path, icon);
|
|
|
|
m_resultEmitter.emitResultsReady(m_path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_resultEmitter.emitResultsFailed(m_path);
|
|
|
|
}
|
|
|
|
QString m_path;
|
|
|
|
SharedIconCachePtr m_cache;
|
|
|
|
ThumbnailingResult m_resultEmitter;
|
|
|
|
};
|
|
|
|
|
2014-07-12 21:02:52 +00:00
|
|
|
// this is about as elegant and well written as a bag of bricks with scribbles done by insane
|
|
|
|
// asylum patients.
|
2014-06-28 15:09:23 +00:00
|
|
|
class FilterModel : public QIdentityProxyModel
|
|
|
|
{
|
2014-07-05 11:26:37 +00:00
|
|
|
Q_OBJECT
|
2014-06-30 00:02:57 +00:00
|
|
|
public:
|
2014-07-12 21:02:52 +00:00
|
|
|
explicit FilterModel(QObject *parent = 0) : QIdentityProxyModel(parent)
|
2014-07-05 11:26:37 +00:00
|
|
|
{
|
|
|
|
m_thumbnailingPool.setMaxThreadCount(4);
|
|
|
|
m_thumbnailCache = std::make_shared<SharedIconCache>();
|
2015-03-01 21:20:57 +00:00
|
|
|
m_thumbnailCache->add("placeholder", MMC->getThemedIcon("screenshot-placeholder"));
|
2014-07-05 11:26:37 +00:00
|
|
|
connect(&watcher, SIGNAL(fileChanged(QString)), SLOT(fileChanged(QString)));
|
|
|
|
// FIXME: the watched file set is not updated when files are removed
|
|
|
|
}
|
2014-07-12 21:02:52 +00:00
|
|
|
virtual ~FilterModel() { m_thumbnailingPool.waitForDone(500); }
|
2014-06-28 15:09:23 +00:00
|
|
|
virtual QVariant data(const QModelIndex &proxyIndex, int role = Qt::DisplayRole) const
|
|
|
|
{
|
|
|
|
auto model = sourceModel();
|
2014-06-30 00:02:57 +00:00
|
|
|
if (!model)
|
2014-06-28 15:09:23 +00:00
|
|
|
return QVariant();
|
2014-06-30 00:02:57 +00:00
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole)
|
|
|
|
{
|
|
|
|
QVariant result = sourceModel()->data(mapToSource(proxyIndex), role);
|
|
|
|
return result.toString().remove(QRegExp("\\.png$"));
|
|
|
|
}
|
|
|
|
if (role == Qt::DecorationRole)
|
2014-06-28 15:09:23 +00:00
|
|
|
{
|
2014-07-12 21:02:52 +00:00
|
|
|
QVariant result =
|
|
|
|
sourceModel()->data(mapToSource(proxyIndex), QFileSystemModel::FilePathRole);
|
2014-06-30 00:02:57 +00:00
|
|
|
QString filePath = result.toString();
|
2014-07-05 11:26:37 +00:00
|
|
|
QIcon temp;
|
2014-07-12 21:02:52 +00:00
|
|
|
if (!watched.contains(filePath))
|
2014-06-30 00:02:57 +00:00
|
|
|
{
|
2014-07-05 11:26:37 +00:00
|
|
|
((QFileSystemWatcher &)watcher).addPath(filePath);
|
|
|
|
((QSet<QString> &)watched).insert(filePath);
|
2014-06-30 00:02:57 +00:00
|
|
|
}
|
2014-07-12 21:02:52 +00:00
|
|
|
if (m_thumbnailCache->get(filePath, temp))
|
2014-06-30 00:02:57 +00:00
|
|
|
{
|
2014-07-05 11:26:37 +00:00
|
|
|
return temp;
|
2014-06-30 00:02:57 +00:00
|
|
|
}
|
2014-07-12 21:02:52 +00:00
|
|
|
if (!m_failed.contains(filePath))
|
2014-07-05 11:26:37 +00:00
|
|
|
{
|
|
|
|
((FilterModel *)this)->thumbnailImage(filePath);
|
|
|
|
}
|
2014-07-12 21:02:52 +00:00
|
|
|
return (m_thumbnailCache->get("placeholder"));
|
2014-06-28 15:09:23 +00:00
|
|
|
}
|
2014-07-05 11:26:37 +00:00
|
|
|
return sourceModel()->data(mapToSource(proxyIndex), role);
|
2014-06-28 15:09:23 +00:00
|
|
|
}
|
|
|
|
virtual bool setData(const QModelIndex &index, const QVariant &value,
|
2014-06-30 00:02:57 +00:00
|
|
|
int role = Qt::EditRole)
|
2014-06-28 15:09:23 +00:00
|
|
|
{
|
|
|
|
auto model = sourceModel();
|
2014-06-30 00:02:57 +00:00
|
|
|
if (!model)
|
2014-06-28 15:09:23 +00:00
|
|
|
return false;
|
2014-06-30 00:02:57 +00:00
|
|
|
if (role != Qt::EditRole)
|
2014-06-28 15:09:23 +00:00
|
|
|
return false;
|
|
|
|
// FIXME: this is a workaround for a bug in QFileSystemModel, where it doesn't
|
|
|
|
// sort after renames
|
|
|
|
{
|
|
|
|
((QFileSystemModel *)model)->setNameFilterDisables(true);
|
|
|
|
((QFileSystemModel *)model)->setNameFilterDisables(false);
|
|
|
|
}
|
|
|
|
return model->setData(mapToSource(index), value.toString() + ".png", role);
|
|
|
|
}
|
2014-07-12 21:02:52 +00:00
|
|
|
|
2014-06-30 00:02:57 +00:00
|
|
|
private:
|
2014-07-05 11:26:37 +00:00
|
|
|
void thumbnailImage(QString path)
|
|
|
|
{
|
|
|
|
auto runnable = new ThumbnailRunnable(path, m_thumbnailCache);
|
2014-07-12 21:02:52 +00:00
|
|
|
connect(&(runnable->m_resultEmitter), SIGNAL(resultsReady(QString)),
|
|
|
|
SLOT(thumbnailReady(QString)));
|
|
|
|
connect(&(runnable->m_resultEmitter), SIGNAL(resultsFailed(QString)),
|
|
|
|
SLOT(thumbnailFailed(QString)));
|
2014-07-05 11:26:37 +00:00
|
|
|
((QThreadPool &)m_thumbnailingPool).start(runnable);
|
|
|
|
}
|
2014-07-12 21:02:52 +00:00
|
|
|
private slots:
|
|
|
|
void thumbnailReady(QString path) { emit layoutChanged(); }
|
|
|
|
void thumbnailFailed(QString path) { m_failed.insert(path); }
|
2014-07-05 11:26:37 +00:00
|
|
|
void fileChanged(QString filepath)
|
|
|
|
{
|
|
|
|
m_thumbnailCache->setStale(filepath);
|
|
|
|
thumbnailImage(filepath);
|
|
|
|
// reinsert the path...
|
|
|
|
watcher.removePath(filepath);
|
|
|
|
watcher.addPath(filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SharedIconCachePtr m_thumbnailCache;
|
|
|
|
QThreadPool m_thumbnailingPool;
|
|
|
|
QSet<QString> m_failed;
|
|
|
|
QSet<QString> watched;
|
|
|
|
QFileSystemWatcher watcher;
|
2014-06-28 15:09:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CenteredEditingDelegate : public QStyledItemDelegate
|
|
|
|
{
|
|
|
|
public:
|
2014-07-12 21:02:52 +00:00
|
|
|
explicit CenteredEditingDelegate(QObject *parent = 0) : QStyledItemDelegate(parent) {}
|
|
|
|
virtual ~CenteredEditingDelegate() {}
|
2014-06-28 15:09:23 +00:00
|
|
|
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
auto widget = QStyledItemDelegate::createEditor(parent, option, index);
|
2014-06-30 00:02:57 +00:00
|
|
|
auto foo = dynamic_cast<QLineEdit *>(widget);
|
|
|
|
if (foo)
|
2014-06-28 15:09:23 +00:00
|
|
|
{
|
|
|
|
foo->setAlignment(Qt::AlignHCenter);
|
|
|
|
foo->setFrame(true);
|
|
|
|
foo->setMaximumWidth(192);
|
|
|
|
}
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-27 21:31:07 +00:00
|
|
|
ScreenshotsPage::ScreenshotsPage(QString path, QWidget *parent)
|
2014-06-28 15:09:23 +00:00
|
|
|
: QWidget(parent), ui(new Ui::ScreenshotsPage)
|
|
|
|
{
|
|
|
|
m_model.reset(new QFileSystemModel());
|
|
|
|
m_filterModel.reset(new FilterModel());
|
|
|
|
m_filterModel->setSourceModel(m_model.get());
|
|
|
|
m_model->setFilter(QDir::Files | QDir::Writable | QDir::Readable);
|
|
|
|
m_model->setReadOnly(false);
|
2014-07-05 11:26:37 +00:00
|
|
|
m_model->setNameFilters({"*.png"});
|
|
|
|
m_model->setNameFilterDisables(false);
|
2015-01-27 21:31:07 +00:00
|
|
|
m_folder = path;
|
2015-10-04 23:47:27 +00:00
|
|
|
m_valid = FS::ensureFolderPathExists(m_folder);
|
2014-06-28 15:09:23 +00:00
|
|
|
|
|
|
|
ui->setupUi(this);
|
2014-07-20 22:10:13 +00:00
|
|
|
ui->tabWidget->tabBar()->hide();
|
2014-06-28 15:09:23 +00:00
|
|
|
ui->listView->setIconSize(QSize(128, 128));
|
2014-07-05 11:26:37 +00:00
|
|
|
ui->listView->setGridSize(QSize(192, 160));
|
2014-06-28 15:09:23 +00:00
|
|
|
ui->listView->setSpacing(9);
|
|
|
|
// ui->listView->setUniformItemSizes(true);
|
|
|
|
ui->listView->setLayoutMode(QListView::Batched);
|
|
|
|
ui->listView->setViewMode(QListView::IconMode);
|
|
|
|
ui->listView->setResizeMode(QListView::Adjust);
|
|
|
|
ui->listView->installEventFilter(this);
|
|
|
|
ui->listView->setEditTriggers(0);
|
|
|
|
ui->listView->setItemDelegate(new CenteredEditingDelegate(this));
|
|
|
|
connect(ui->listView, SIGNAL(activated(QModelIndex)), SLOT(onItemActivated(QModelIndex)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScreenshotsPage::eventFilter(QObject *obj, QEvent *evt)
|
|
|
|
{
|
|
|
|
if (obj != ui->listView)
|
|
|
|
return QWidget::eventFilter(obj, evt);
|
|
|
|
if (evt->type() != QEvent::KeyPress)
|
|
|
|
{
|
|
|
|
return QWidget::eventFilter(obj, evt);
|
|
|
|
}
|
|
|
|
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(evt);
|
|
|
|
switch (keyEvent->key())
|
|
|
|
{
|
|
|
|
case Qt::Key_Delete:
|
|
|
|
on_deleteBtn_clicked();
|
|
|
|
return true;
|
|
|
|
case Qt::Key_F2:
|
|
|
|
on_renameBtn_clicked();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QWidget::eventFilter(obj, evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
ScreenshotsPage::~ScreenshotsPage()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenshotsPage::onItemActivated(QModelIndex index)
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
auto info = m_model->fileInfo(index);
|
|
|
|
QString fileName = info.absoluteFilePath();
|
2016-01-05 06:32:52 +00:00
|
|
|
DesktopServices::openFile(info.absoluteFilePath());
|
2014-06-28 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenshotsPage::on_viewFolderBtn_clicked()
|
|
|
|
{
|
2016-01-05 06:32:52 +00:00
|
|
|
DesktopServices::openDirectory(m_folder, true);
|
2014-06-28 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenshotsPage::on_uploadBtn_clicked()
|
|
|
|
{
|
2017-08-19 21:14:06 +00:00
|
|
|
auto selection = ui->listView->selectionModel()->selectedRows();
|
2014-06-28 15:09:23 +00:00
|
|
|
if (selection.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QList<ScreenshotPtr> uploaded;
|
2015-04-05 12:50:58 +00:00
|
|
|
auto job = NetJobPtr(new NetJob("Screenshot Upload"));
|
2014-06-28 15:09:23 +00:00
|
|
|
for (auto item : selection)
|
|
|
|
{
|
|
|
|
auto info = m_model->fileInfo(item);
|
|
|
|
auto screenshot = std::make_shared<ScreenShot>(info);
|
|
|
|
uploaded.push_back(screenshot);
|
|
|
|
job->addNetAction(ImgurUpload::make(screenshot));
|
|
|
|
}
|
|
|
|
SequentialTask task;
|
2015-04-05 12:50:58 +00:00
|
|
|
auto albumTask = NetJobPtr(new NetJob("Imgur Album Creation"));
|
2014-06-28 15:09:23 +00:00
|
|
|
auto imgurAlbum = ImgurAlbumCreation::make(uploaded);
|
|
|
|
albumTask->addNetAction(imgurAlbum);
|
2015-04-05 12:50:58 +00:00
|
|
|
task.addTask(job.unwrap());
|
|
|
|
task.addTask(albumTask.unwrap());
|
2015-12-03 22:00:51 +00:00
|
|
|
m_uploadActive = true;
|
2014-06-28 15:09:23 +00:00
|
|
|
ProgressDialog prog(this);
|
2015-09-26 02:04:09 +00:00
|
|
|
if (prog.execWithTask(&task) != QDialog::Accepted)
|
2014-06-28 15:09:23 +00:00
|
|
|
{
|
|
|
|
CustomMessageBox::selectable(this, tr("Failed to upload screenshots!"),
|
|
|
|
tr("Unknown error"), QMessageBox::Warning)->exec();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-06 22:02:04 +00:00
|
|
|
auto link = QString("https://imgur.com/a/%1").arg(imgurAlbum->id());
|
|
|
|
QClipboard *clipboard = QApplication::clipboard();
|
|
|
|
clipboard->setText(link);
|
2014-06-28 15:09:23 +00:00
|
|
|
CustomMessageBox::selectable(
|
2016-11-26 13:37:36 +00:00
|
|
|
this,
|
|
|
|
tr("Upload finished"),
|
|
|
|
tr("The <a href=\"%1\">link to the uploaded album</a> has been placed in your clipboard.") .arg(link),
|
|
|
|
QMessageBox::Information
|
|
|
|
)->exec();
|
2014-06-28 15:09:23 +00:00
|
|
|
}
|
2015-12-03 22:00:51 +00:00
|
|
|
m_uploadActive = false;
|
2014-06-28 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenshotsPage::on_deleteBtn_clicked()
|
|
|
|
{
|
|
|
|
auto mbox = CustomMessageBox::selectable(
|
|
|
|
this, tr("Are you sure?"), tr("This will delete all selected screenshots."),
|
|
|
|
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No);
|
|
|
|
std::unique_ptr<QMessageBox> box(mbox);
|
|
|
|
|
2014-06-30 00:02:57 +00:00
|
|
|
if (box->exec() != QMessageBox::Yes)
|
2014-06-28 15:09:23 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto selected = ui->listView->selectionModel()->selectedIndexes();
|
2014-06-30 00:02:57 +00:00
|
|
|
for (auto item : selected)
|
2014-06-28 15:09:23 +00:00
|
|
|
{
|
|
|
|
m_model->remove(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenshotsPage::on_renameBtn_clicked()
|
|
|
|
{
|
|
|
|
auto selection = ui->listView->selectionModel()->selectedIndexes();
|
|
|
|
if (selection.isEmpty())
|
|
|
|
return;
|
|
|
|
ui->listView->edit(selection[0]);
|
|
|
|
// TODO: mass renaming
|
|
|
|
}
|
|
|
|
|
2018-03-19 01:36:12 +00:00
|
|
|
void ScreenshotsPage::openedImpl()
|
2014-06-28 15:09:23 +00:00
|
|
|
{
|
2016-11-21 23:56:48 +00:00
|
|
|
if(!m_valid)
|
|
|
|
{
|
|
|
|
m_valid = FS::ensureFolderPathExists(m_folder);
|
|
|
|
}
|
2014-06-28 15:09:23 +00:00
|
|
|
if (m_valid)
|
|
|
|
{
|
|
|
|
QString path = QDir(m_folder).absolutePath();
|
2016-11-21 23:56:48 +00:00
|
|
|
auto idx = m_model->setRootPath(path);
|
|
|
|
if(idx.isValid())
|
|
|
|
{
|
|
|
|
ui->listView->setModel(m_filterModel.get());
|
|
|
|
ui->listView->setRootIndex(m_filterModel->mapFromSource(idx));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->listView->setModel(nullptr);
|
|
|
|
}
|
2014-06-28 15:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-05 11:26:37 +00:00
|
|
|
|
|
|
|
#include "ScreenshotsPage.moc"
|