Skip to content

Commit

Permalink
fix: 优化处理细节;裁切选取边框会自动选择对比色;修复多语言显示的 Bug ;
Browse files Browse the repository at this point in the history
  • Loading branch information
Wing-summer committed Oct 20, 2024
1 parent fc64c20 commit fe51f0f
Show file tree
Hide file tree
Showing 18 changed files with 307 additions and 157 deletions.
Binary file removed images/fitinview.png
Binary file not shown.
261 changes: 137 additions & 124 deletions lang/WingGifEditor2_zh_CN.ts

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<file>images/export.png</file>
<file>images/file.png</file>
<file>images/first.png</file>
<file>images/fitinview.png</file>
<file>images/fliph.png</file>
<file>images/flipv.png</file>
<file>images/foreword.png</file>
Expand Down
4 changes: 4 additions & 0 deletions src/class/gifcontentmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ void GifContentModel::setLinkedListView(QListView *view) {
_view = view;
}

void GifContentModel::setLinkedEditor(GifEditor *editor) { _editor = editor; }

QListView *GifContentModel::linkedListView() const { return _view; }

GifEditor *GifContentModel::linkedGifEditor() const { return _editor; }

void GifContentModel::updateLinkedListViewCurrent() const {
if (_view) {
auto i = _view->currentIndex();
Expand Down
6 changes: 6 additions & 0 deletions src/class/gifcontentmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QListView>
#include <QStyledItemDelegate>

#include "control/gifeditor.h"
#include "utilities.h"

class GifContentModel : public QAbstractListModel {
Expand All @@ -19,8 +20,12 @@ class GifContentModel : public QAbstractListModel {

void setLinkedListView(QListView *view);

void setLinkedEditor(GifEditor *editor);

QListView *linkedListView() const;

GifEditor *linkedGifEditor() const;

QImage image(qsizetype index) const;

int delay(qsizetype index) const;
Expand Down Expand Up @@ -105,6 +110,7 @@ public slots:
QVector<int> _delays;

QListView *_view = nullptr;
GifEditor *_editor = nullptr;
};

#endif // GIFCONTENTMODEL_H
2 changes: 1 addition & 1 deletion src/class/languagemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ LanguageManager::LanguageManager() {
#else
if (p->country() == _defaultLocale.country() &&
#endif
p->language() == p->language() &&
p->language() == _defaultLocale.language() &&
p->script() == _defaultLocale.script()) {
found = true;
break;
Expand Down
24 changes: 20 additions & 4 deletions src/class/picturedelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#include <QPainter>

const unsigned int BANNER_HEIGHT = 20;
const unsigned int BANNER_COLOR = 0x303030;
const unsigned int BANNER_ALPHA = 200;
const unsigned int BANNER_TEXT_COLOR = 0xffffff;
const unsigned int HIGHLIGHT_ALPHA = 100;

PictureDelegate::PictureDelegate(QObject *parent)
Expand All @@ -29,13 +27,13 @@ void PictureDelegate::paint(QPainter *painter,
QRect bannerRectBottom = bannerRectTop;
bannerRectBottom.moveBottom(option.rect.bottom());

QColor bannerColor = QColor(BANNER_COLOR);
QColor bannerColor = this->bannerColor;
bannerColor.setAlpha(BANNER_ALPHA);
painter->fillRect(bannerRectTop, bannerColor);
painter->fillRect(bannerRectBottom, bannerColor);

QString filename = index.model()->data(index, Qt::DisplayRole).toString();
painter->setPen(BANNER_TEXT_COLOR);
painter->setPen(this->bannerTextColor);
painter->drawText(bannerRectTop, Qt::AlignCenter,
QString::number(index.row() + 1));
painter->drawText(bannerRectBottom, Qt::AlignCenter, filename);
Expand All @@ -54,3 +52,21 @@ QSize PictureDelegate::sizeHint(const QStyleOptionViewItem &option,
auto s = option.widget->height() - 40;
return QSize(s * 5 / 4, s);
}

QColor PictureDelegate::getBannerTextColor() const { return bannerTextColor; }

void PictureDelegate::setBannerTextColor(const QColor &newBannerTextColor) {
if (bannerTextColor == newBannerTextColor)
return;
bannerTextColor = newBannerTextColor;
emit bannerTextColorChanged();
}

QColor PictureDelegate::getBannerColor() const { return bannerColor; }

void PictureDelegate::setBannerColor(const QColor &newBannerColor) {
if (bannerColor == newBannerColor)
return;
bannerColor = newBannerColor;
emit bannerColorChanged();
}
20 changes: 20 additions & 0 deletions src/class/picturedelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,33 @@

class PictureDelegate : public QStyledItemDelegate {
Q_OBJECT

Q_PROPERTY(QColor bannerTextColor READ getBannerTextColor WRITE
setBannerTextColor NOTIFY bannerTextColorChanged FINAL)
Q_PROPERTY(QColor bannerColor READ getBannerColor WRITE setBannerColor
NOTIFY bannerColorChanged FINAL)
public:
PictureDelegate(QObject *parent = nullptr);

void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const override;

QColor getBannerTextColor() const;
void setBannerTextColor(const QColor &newBannerTextColor);

QColor getBannerColor() const;
void setBannerColor(const QColor &newBannerColor);

signals:
void bannerTextColorChanged();

void bannerColorChanged();

private:
QColor bannerTextColor = 0xffffff;
QColor bannerColor = 0x303030;
};

#endif // PICTUREDELEGATE_H
10 changes: 8 additions & 2 deletions src/command/cropimagecommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ CropImageCommand::CropImageCommand(GifContentModel *helper, const QRect &rect,
buffer = helper->images();
}

void CropImageCommand::undo() { gif->swapFrames(buffer); }
void CropImageCommand::undo() {
gif->swapFrames(buffer);
gif->linkedGifEditor()->fitOpenSize();
}

void CropImageCommand::redo() { gif->cropFrames(_rect); }
void CropImageCommand::redo() {
gif->cropFrames(_rect);
gif->linkedGifEditor()->fitOpenSize();
}
10 changes: 8 additions & 2 deletions src/command/rotateframecommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ RotateFrameCommand::RotateFrameCommand(GifContentModel *helper,
bool isclockwise, QUndoCommand *parent)
: QUndoCommand(parent), gif(helper), clockwise(isclockwise) {}

void RotateFrameCommand::undo() { gif->rotateFrames(!clockwise); }
void RotateFrameCommand::undo() {
gif->rotateFrames(!clockwise);
gif->linkedGifEditor()->fitOpenSize();
}

void RotateFrameCommand::redo() { gif->rotateFrames(clockwise); }
void RotateFrameCommand::redo() {
gif->rotateFrames(clockwise);
gif->linkedGifEditor()->fitOpenSize();
}
2 changes: 2 additions & 0 deletions src/control/gifeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ void GifEditor::fitOpenSize() {
if (imgSize.width() > wSize.width() || imgSize.height() > wSize.height()) {
fitInEditorView();
} else {
auto r = scene->contentBounding();
setSceneRect(r);
resetTransform();
}
}
Expand Down
55 changes: 53 additions & 2 deletions src/control/gifeditorscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@
#include <QGraphicsProxyWidget>
#include <QGraphicsSceneMouseEvent>

#include <QAtomicInt>
#include <QColor>
#include <QHash>

size_t qHash(const QColor &color, size_t seed = 0) noexcept {
return qHash(qMakePair(color.rgba(), color.spec()), seed);
}

GifEditorScene::GifEditorScene(const QImage &img, QObject *parent)
: QGraphicsScene(parent) {
sel = new ImageCropper();
sel->setAttribute(Qt::WA_TranslucentBackground);
sel->setBackgroundColor(Qt::transparent);
sel->setCropperVisible(false);
sel->setImage(img);
sel->setCroppingRectBorderColor(Qt::gray);
setFrameImg(img);
gw = addWidget(sel);
connect(sel, &ImageCropper::sizeChanged, gw, [=] {
// call the internal updateProxyGeometryFromWidget
Expand All @@ -30,7 +39,13 @@ void GifEditorScene::setCuttingMode(bool value) {
sel->setCropperVisible(value);
}

void GifEditorScene::setFrameImg(const QImage &img) { sel->setImage(img); }
void GifEditorScene::setFrameImg(const QImage &img) {
sel->setImage(img);
auto mainColor = getMainThemeColor(img);
if (!img.isNull()) {
sel->setCroppingRectBorderColor(getContrastingColor(mainColor));
}
}

QRectF GifEditorScene::contentBounding() const { return gw->boundingRect(); }

Expand All @@ -41,3 +56,39 @@ QRectF GifEditorScene::selRect() const { return sel->selRect(); }
void GifEditorScene::setSelRect(int x, int y, int w, int h) {
sel->setSelRect(x, y, w, h);
}

double GifEditorScene::getLuminance(const QColor &color) {
return 0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue();
}

QColor GifEditorScene::getContrastingColor(const QColor &color) {
double luminance = getLuminance(color);
return (luminance > 128) ? QColor(Qt::black) : QColor(Qt::white);
}

QColor GifEditorScene::getMainThemeColor(const QImage &image) {
QHash<QColor, QAtomicInt> colorCount;

// Reduce the image size to speed up the process
QImage scaledImage = image.scaled(50, 50);

// Count each pixel's color
for (int y = 0; y < scaledImage.height(); ++y) {
for (int x = 0; x < scaledImage.width(); ++x) {
QColor color = scaledImage.pixelColor(x, y);
colorCount[color]++;
}
}

// Find the most frequent color
QColor dominantColor;
int maxCount = 0;
for (auto it = colorCount.constBegin(); it != colorCount.constEnd(); ++it) {
if (it.value() > maxCount) {
maxCount = it.value();
dominantColor = it.key();
}
}

return dominantColor;
}
10 changes: 10 additions & 0 deletions src/control/gifeditorscene.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public slots:
signals:
void selRectChanged(const QRectF &rect);

private:
// Function to calculate luminance of a color
double getLuminance(const QColor &color);

// Function to get the contrasting color (white or black)
QColor getContrastingColor(const QColor &color);

// Function to get the main theme color from the image
QColor getMainThemeColor(const QImage &image);

private:
ImageCropper *sel;
QPointF oldpos;
Expand Down
1 change: 0 additions & 1 deletion src/control/imagecropper/imagecropper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ void ImageCropper::setSelRect(int x, int y, int w, int h) {
void ImageCropper::setImage(const QImage &_image) {
pimpl->imageForCropping = _image;
setFixedSize(_image.size());

update();
}

Expand Down
4 changes: 4 additions & 0 deletions src/dialog/cropgifdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <QPushButton>

CropGifDialog::CropGifDialog(QWidget *parent) : FramelessDialogBase(parent) {
setAttribute(Qt::WA_ShowWithoutActivating);

auto widget = new QWidget(this);
auto layout = new QVBoxLayout(widget);

Expand Down Expand Up @@ -64,6 +66,8 @@ CropGifDialog::CropGifDialog(QWidget *parent) : FramelessDialogBase(parent) {
layout->addWidget(btnBox);
setFocusPolicy(Qt::StrongFocus);

widget->setMinimumWidth(300);

buildUpContent(widget);

setWindowTitle(tr("CropGifDialog"));
Expand Down
2 changes: 2 additions & 0 deletions src/dialog/logdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ LogDialog::LogDialog(QWidget *parent) : FramelessDialogBase(parent) {
_log->setOpenExternalLinks(true);
_log->setUndoRedoEnabled(false);

setWindowTitle(tr("Log"));

buildUpContent(_log);
}

Expand Down
Loading

0 comments on commit fe51f0f

Please sign in to comment.