Skip to content

Commit

Permalink
[feature] refine HistoryWindow design
Browse files Browse the repository at this point in the history
  • Loading branch information
klesh committed Jun 27, 2019
1 parent 7a9ae9f commit b4e2743
Show file tree
Hide file tree
Showing 25 changed files with 410 additions and 133 deletions.
1 change: 1 addition & 0 deletions fu.pro
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ HEADERS += \
src/core/tagservice.h \
src/core/uploadservice.h \
src/core/uploadthread.h \
src/core/utils.h \
src/historywindow.h \
src/application.h \
src/models/clip.h \
Expand Down
5 changes: 5 additions & 0 deletions fu.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
<file>icons/icon-180.png</file>
<file>icons/unknown-icon.png</file>
<file>icons/icon-uploading-32.png</file>
<file>icons/x.svg</file>
<file>icons/check-square.svg</file>
<file>icons/trash.svg</file>
<file>icons/x-square.svg</file>
<file>icons/file.svg</file>
</qresource>
</RCC>
1 change: 1 addition & 0 deletions icons/check-square.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions icons/file.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions icons/trash.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions icons/x-square.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions icons/x.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/application.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef APPLICATION_H
#define APPLICATION_H

#include "core/utils.h"
#include "components/errormessage.h"
#include "upgradedialog.h"
#include "aboutdialog.h"
Expand Down
16 changes: 8 additions & 8 deletions src/components/previewbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ PreviewBox::PreviewBox(QWidget *parent)
previewImg = new ThumbnailLabel(this);
boxLayout->addWidget(previewImg);

uploadedTo = new QLabel(this);
uploadedTo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
uploadedTo->setMaximumHeight(25);
uploadedTo->setText(tr("Uploaded to: ") + "haha");
uploadedTo->setAlignment(Qt::AlignHCenter);
boxLayout->addWidget(uploadedTo);
name = new QLabel(this);
name->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
name->setMaximumHeight(25);
name->setText(tr("Uploaded to: ") + "haha");
name->setAlignment(Qt::AlignHCenter);
boxLayout->addWidget(name);

tagsFrame = nullptr;

Expand All @@ -53,9 +53,9 @@ void PreviewBox::setImage(const QPixmap &thumbnail)
previewImg->setPixmap(thumbnail);
}

void PreviewBox::setUploadedTo(const QString &serverName)
void PreviewBox::setName(const QString &n)
{
uploadedTo->setText(serverName);
name->setText(n);
}

void PreviewBox::setTags(const QStringList &tags)
Expand Down
4 changes: 2 additions & 2 deletions src/components/previewbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class PreviewBox : public QFrame
//~PreviewBox();

void setImage(const QPixmap &thumbnail);
void setUploadedTo(const QString &serverName);
void setName(const QString &name);
void setTags(const QStringList &tags);
bool isSelected();

protected:
void mousePressEvent(QMouseEvent *evt) override;
QLabel *previewImg;
QLabel *uploadedTo;
QLabel *name;
QFrame *tagsFrame;
bool selected;
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/thumbnaillabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ThumbnailLabel::ThumbnailLabel(QWidget *parent)
setMaximumSize(THUMB_WIDTH, THUMB_HEIGHT);

setAlignment(Qt::AlignCenter);
setStyleSheet("background: white");
//setStyleSheet("background: white");
setPixmap(ClipService::unkownFileIcon());
}

Expand Down
90 changes: 89 additions & 1 deletion src/core/clipservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@

#include <QClipboard>

Clip convertResultToClip(QSqlQuery &result) {
Clip clip;
auto rec = result.record();
clip.id = result.value(rec.indexOf("id")).toUInt();
clip.name = result.value(rec.indexOf("name")).toString();
clip.isImage = result.value(rec.indexOf("isImage")).toBool();
clip.isFile = result.value(rec.indexOf("isFile")).toBool();
clip.rawPngThumb = result.value(rec.indexOf("preview")).toByteArray();
clip.description = result.value(rec.indexOf("description")).toString();
clip.createdAt = result.value(rec.indexOf("createdAt")).toDateTime();
return clip;
}

ClipService::ClipService(SqlStore &store)
: _store(store)
{
Expand Down Expand Up @@ -52,14 +65,89 @@ void ClipService::setClipboard(const QString &text)
QApplication::clipboard()->setText(text);
}

QList<Clip> ClipService::search(QMap<QString, QVariant> &filter)
{
QList<Clip> clips;

QStringList sql, where;
sql.append("SELECT clips.* FROM clips");


if (filter.contains("dateFrom")) {
where.append(QString("DATETIME(clips.createdAt) > DATETIME('%1')").arg(dateToISO(filter["dateFrom"].toDate())));
}
if (filter.contains("dateTo")) {
where.append(QString("DATETIME(clips.createdAt) <= DATETIME('%1')").arg(dateToISO(filter["dateTo"].toDate().addDays(1))));
}
if (filter.contains("serverIds")) {
auto serverIds = qvariant_cast<QList<uint>>(filter["serverIds"]);
sql.append("LEFT JOIN uploads ON clips.id=uploads.clipId");
where.append(QString("uploads.serverId IN (%1)").arg(join<uint>(serverIds)));
}
if (filter.contains("tags")) {
auto tags = qvariant_cast<QStringList>(filter["tags"]);
auto tagIds = APP->tagService()->mapToIds(tags);
sql.append("LEFT JOIN clips_tags ON clips.id=clips_tags.clipId");
where.append(QString("clips_tags.tagId IN (%1)").arg(join<uint>(tagIds)));
}

if (!where.empty()) {
sql.append("WHERE");
sql.append(where.join(" AND "));
}

sql.append("ORDER BY id DESC");
auto sqlText = sql.join(" ");

qDebug() << sqlText;

auto result = _store.exec(sqlText);
while (result.next()) {
clips.append(convertResultToClip(result));
}

for (auto &clip : clips) {
auto tagsResult = _store.exec(QString("SELECT tags.name FROM tags LEFT JOIN clips_tags ON (tags.id=clips_tags.tagId) WHERE clips_tags.clipId=%1").arg(clip.id));
while (tagsResult.next()) {
clip.tags.append(tagsResult.value(0).toString());
}
}

return clips;
}

QList<QPair<QDate, QList<Clip>>> ClipService::searchAndGroup(QMap<QString, QVariant> &filter)
{
auto clips = search(filter);
auto datedClips = groupByCreationDate(clips);
return datedClips;
}

QPixmap ClipService::thumbnailize(const QPixmap &origin)
{
return origin.scaled(THUMB_WIDTH, THUMB_HEIGHT, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}

const QPixmap &ClipService::unkownFileIcon()
{
const static QPixmap unknownImg(":icons/unknown-icon.png");
const static QPixmap unknownImg(":icons/file.svg");
return unknownImg;
}

QList<QPair<QDate, QList<Clip>>> ClipService::groupByCreationDate(QList<Clip> &clips)
{
QList<QPair<QDate, QList<Clip>>> datedClips;
for (auto &clip : clips) {
QDate date = clip.createdAt.date();
if (datedClips.empty() || datedClips.last().first != date) {
//if (!datedClips.contains(date)) {
QPair<QDate, QList<Clip>> pair;
QList<Clip> clips;
pair.first = date;
pair.second = clips;
datedClips.append(pair);
}
datedClips.last().second.append(clip);
}
return datedClips;
}
3 changes: 3 additions & 0 deletions src/core/clipservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ class ClipService

QList<Clip> getAllFromClipboard();
void setClipboard(const QString &text);
QList<Clip> search(QMap<QString, QVariant> &filter);
QList<QPair<QDate, QList<Clip>>> searchAndGroup(QMap<QString, QVariant> &filter);
static QPixmap thumbnailize(const QPixmap &origin);
const static QPixmap &unkownFileIcon();
static QList<QPair<QDate, QList<Clip>>> groupByCreationDate(QList<Clip> &clips);
};

#endif // CLIPSERVICE_H
2 changes: 1 addition & 1 deletion src/core/outputformatservice.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "error.h"
#include "outputformatservice.h"

OutputFormat convertResultToOutputFormat(QSqlQuery result) {
OutputFormat convertResultToOutputFormat(QSqlQuery &result) {
OutputFormat outputFormat;
auto rec = result.record();
outputFormat.id = result.value(rec.indexOf("id")).toUInt();
Expand Down
8 changes: 4 additions & 4 deletions src/core/serverservice.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../application.h"
#include "error.h"
#include "serverservice.h"
#include "error.h"
#include "../application.h"
#include "../protocols/localstorageprotocol.h"
#include "../protocols/ftpprotocol.h"

Expand All @@ -9,7 +9,7 @@ QString toJson(QVariantMap &settings)
return QJsonDocument::fromVariant(settings).toJson(QJsonDocument::Compact);
}

Server convertResultToServer(QSqlQuery result) {
Server convertResultToServer(QSqlQuery &result) {
Server server;
auto rec = result.record();
server.id = result.value(rec.indexOf("id")).toUInt();
Expand Down Expand Up @@ -74,7 +74,7 @@ void ServerService::append(Server &server)
query.bindValue(":name", server.name);
query.bindValue(":protocol", server.protocol);
query.bindValue(":settings", toJson(server.settings));
query.bindValue(":createdAt", QDateTime::currentDateTime().toString(Qt::ISODate));
query.bindValue(":createdAt", datetimeToISO());

auto result = _store.exec();
server.id = result.lastInsertId().toUInt();
Expand Down
24 changes: 20 additions & 4 deletions src/core/tagservice.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "tagservice.h"


Tag convertResultToTag(QSqlQuery result) {
Tag convertResultToTag(QSqlQuery &result) {
Tag tag;
auto rec = result.record();
tag.id = result.value(rec.indexOf("id")).toUInt();
Expand Down Expand Up @@ -62,9 +62,8 @@ void TagService::remove(uint id)
query.exec();
}

uint TagService::findOrAppend(const QString &name)
uint TagService::find(const QString &name)
{
qDebug() << "find or append " << name;
auto query = _store.prepare("SELECT id FROM tags WHERE name=:name");
query.bindValue(":name", name);

Expand All @@ -73,5 +72,22 @@ uint TagService::findOrAppend(const QString &name)
qDebug() << "tag found: " << result.value(0);
return result.value(0).toUInt();
}
return append(name);
return 0;
}

uint TagService::findOrAppend(const QString &name)
{
auto tagId = find(name);
return tagId || append(name);
}

QList<uint> TagService::mapToIds(const QStringList &tags)
{
QList<uint> tagIds;
for (auto &tag : tags) {
auto tagId = this->find(tag);
if (tagId)
tagIds.append(tagId);
}
return tagIds;
}
2 changes: 2 additions & 0 deletions src/core/tagservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class TagService
uint append(const QString &name);
void update(uint id, const QString &name);
void remove(uint id);
uint find(const QString &name);
uint findOrAppend(const QString &name);
QList<uint> mapToIds(const QStringList &tags);
};

#endif // TAGSERVICE_H
1 change: 0 additions & 1 deletion src/core/uploadservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ void UploadService::upload(QList<Clip> &clips, const QStringList &tags, const QS
tagIds.append(APP->tagService()->findOrAppend(tag));
}

int i = 0;
for (auto &clip : clips) {
clip.description = desc;
auto query = _store.prepare("INSERT INTO clips (name, isImage, isFile, preview, description, createdAt)"
Expand Down
32 changes: 32 additions & 0 deletions src/core/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef UTILS_H
#define UTILS_H

#include <QtCore>

inline QString datetimeToISO(const QDateTime &dt = QDateTime::currentDateTime()) {
QDateTime dt2(dt);
dt2.setOffsetFromUtc(dt.offsetFromUtc());
return dt2.toString(Qt::ISODate);
}

inline QDateTime dateToDateTime(const QDate &date = QDate::currentDate()) {
QTime time(0,0,0);
QDateTime datetime(date, time);
return datetime;
}

inline QString dateToISO(const QDate &date = QDate::currentDate()) {
return datetimeToISO(dateToDateTime(date));
}

template <typename T>
inline QString join(const QList<T> list, const QString &sep = ", ") {
QStringList tmp;
for (auto &item : list) {
qDebug() << item;
tmp.append(QString("%1").arg(item));
}
return tmp.join(sep);
}

#endif // UTILS_H
Loading

0 comments on commit b4e2743

Please sign in to comment.