Skip to content

Commit

Permalink
Expose image sheet actions for menu bar
Browse files Browse the repository at this point in the history
  • Loading branch information
kefir500 committed Oct 4, 2021
1 parent 102414a commit f09fde9
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 88 deletions.
39 changes: 39 additions & 0 deletions src/base/actionprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,45 @@ QAction *ActionProvider::getReplace(QWidget *parent) const
return action;
}

QAction *ActionProvider::getZoomIn(QWidget *parent) const
{
auto action = new QAction(QIcon::fromTheme("zoom-in"), {}, parent);
action->setShortcut(QKeySequence::ZoomIn);
action->setShortcutContext(Qt::WidgetWithChildrenShortcut);

auto translate = [=]() { action->setText(tr("Zoom In")); };
connect(this, &ActionProvider::languageChanged, action, translate);
translate();

return action;
}

QAction *ActionProvider::getZoomOut(QWidget *parent) const
{
auto action = new QAction(QIcon::fromTheme("zoom-out"), {}, parent);
action->setShortcut(QKeySequence::ZoomOut);
action->setShortcutContext(Qt::WidgetWithChildrenShortcut);

auto translate = [=]() { action->setText(tr("Zoom Out")); };
connect(this, &ActionProvider::languageChanged, action, translate);
translate();

return action;
}

QAction *ActionProvider::getZoomReset(QWidget *parent) const
{
auto action = new QAction(QIcon::fromTheme("zoom-normal"), {}, parent);
action->setShortcut(QKeySequence("Ctrl+/"));
action->setShortcutContext(Qt::WidgetWithChildrenShortcut);

auto translate = [=]() { action->setText(tr("Reset Zoom")); };
connect(this, &ActionProvider::languageChanged, action, translate);
translate();

return action;
}

QAction *ActionProvider::getVisitWebPage(QObject *parent) const
{
auto action = new QAction(QIcon::fromTheme("help-website"), {}, parent);
Expand Down
4 changes: 4 additions & 0 deletions src/base/actionprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class ActionProvider : public QObject
QAction *getFindPrevious(QWidget *parent) const;
QAction *getReplace(QWidget *parent) const;

QAction *getZoomIn(QWidget *parent) const;
QAction *getZoomOut(QWidget *parent) const;
QAction *getZoomReset(QWidget *parent) const;

QAction *getVisitWebPage(QObject *parent = nullptr) const;
QAction *getVisitSourcePage(QObject *parent = nullptr) const;
QAction *getVisitDonatePage(QObject *parent = nullptr) const;
Expand Down
4 changes: 2 additions & 2 deletions src/sheets/basefilesheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ BaseFileSheet::BaseFileSheet(const ResourceModelIndex &index, QWidget *parent) :
connect(actionExplore, &QAction::triggered, this, &BaseFileSheet::explore);

addAction(actionReplace);
addSeparator();
addActionSeparator();
addAction(actionSave);
addAction(actionSaveAs);
addSeparator();
addActionSeparator();
addAction(actionExplore);

retranslate();
Expand Down
2 changes: 1 addition & 1 deletion src/sheets/basesheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void BaseSheet::setSheetIcon(const QIcon &icon)
emit iconChanged(icon);
}

void BaseSheet::addSeparator()
void BaseSheet::addActionSeparator()
{
auto separator = new QAction(this);
separator->setSeparator(true);
Expand Down
2 changes: 1 addition & 1 deletion src/sheets/basesheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BaseSheet : public QWidget
protected:
void setSheetTitle(const QString &title);
void setSheetIcon(const QIcon &icon);
void addSeparator();
void addActionSeparator();

private:
QString title;
Expand Down
2 changes: 1 addition & 1 deletion src/sheets/codesheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ CodeSheet::CodeSheet(const ResourceModelIndex &index, QWidget *parent) : BaseFil

// Initialize actions:

addSeparator();
addActionSeparator();

auto actionFind = app->actions.getFind(this);
addAction(actionFind);
Expand Down
107 changes: 47 additions & 60 deletions src/sheets/imagesheet.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "sheets/imagesheet.h"
#include "base/application.h"
#include "base/fileformatlist.h"
#include "base/utils.h"
#include "windows/dialogs.h"
#include <QAction>
#include <QFormLayout>
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
Expand All @@ -27,25 +29,52 @@ ImageSheet::ImageSheet(const ResourceModelIndex &index, QWidget *parent) : BaseF
view->setScene(scene);
rubberBand = new QRubberBand(QRubberBand::Rectangle, view);

zoomGroup = new ZoomGroup(this);
auto actionZoomIn = app->actions.getZoomIn(this);
auto actionZoomOut = app->actions.getZoomOut(this);
auto actionZoomReset = app->actions.getZoomReset(this);

connect(actionZoomIn, &QAction::triggered, view, &GraphicsView::zoomIn);
connect(actionZoomOut, &QAction::triggered, view, &GraphicsView::zoomOut);
connect(actionZoomReset, &QAction::triggered, view, &GraphicsView::zoomReset);

addActionSeparator();
addAction(actionZoomIn);
addAction(actionZoomOut);
addAction(actionZoomReset);

sizeLabel = new QLabel(this);
sizeValueLabel = new QLabel(this);

zoomLabel = new QLabel(this);
auto zoomValueLabel = new QLabel(this);
auto btnZoomIn = new QToolButton(this);
auto btnZoomOut = new QToolButton(this);
auto btnZoomReset = new QToolButton(this);
btnZoomIn->setDefaultAction(actionZoomIn);
btnZoomOut->setDefaultAction(actionZoomOut);
btnZoomReset->setDefaultAction(actionZoomReset);
auto zoomControlsLayout = new QHBoxLayout;
zoomControlsLayout->setMargin(0);
zoomControlsLayout->addWidget(btnZoomIn);
zoomControlsLayout->addWidget(btnZoomReset);
zoomControlsLayout->addWidget(btnZoomOut);
zoomControlsLayout->addWidget(zoomValueLabel);
zoomControlsLayout->addStretch();

auto controls = new QWidget(this);
auto controlsLayout = new QFormLayout(controls);
sizeLabel = new QLabel(this);
zoomLabel = new QLabel(this);
controlsLayout->addRow(sizeLabel, sizeValueLabel);
controlsLayout->addRow(zoomLabel, zoomGroup);
controlsLayout->addRow(zoomLabel, zoomControlsLayout);

auto layout = new QVBoxLayout(this);
layout->addWidget(view);
layout->addWidget(controls);
layout->setMargin(0);

connect(zoomGroup, &ZoomGroup::zoomIn, view, &GraphicsView::zoomIn);
connect(zoomGroup, &ZoomGroup::zoomOut, view, &GraphicsView::zoomOut);
connect(zoomGroup, &ZoomGroup::zoomReset, view, &GraphicsView::zoomReset);
connect(view, &GraphicsView::zoomed, zoomGroup, &ZoomGroup::setZoomInfo);
connect(view, &GraphicsView::zoomed, this, [zoomValueLabel](qreal factor) {
const int percentage = static_cast<int>(factor * 100);
zoomValueLabel->setText(QString("%1%").arg(percentage));
});

retranslate();
load();
Expand All @@ -65,16 +94,6 @@ bool ImageSheet::load()
return true;
}

void ImageSheet::setImage(const QPixmap &image)
{
scene->clear();
view->zoomReset();
view->setSceneRect(0, 0, image.width(), image.height());
pixmapItem = scene->addPixmap(image);
pixmapItem->setTransformationMode(Qt::SmoothTransformation);
setSizeInfo(image.size());
}

bool ImageSheet::save(const QString &as)
{
if (as.isEmpty()) {
Expand All @@ -99,22 +118,6 @@ bool ImageSheet::saveAs()
return save(destination);
}

void ImageSheet::setSizeInfo(int width, int height)
{
sizeValueLabel->setText(QString("%1x%2").arg(width).arg(height));
}

void ImageSheet::setSizeInfo(const QSize &size)
{
setSizeInfo(size.width(), size.height());
}

void ImageSheet::retranslate()
{
sizeLabel->setText(tr("Size"));
zoomLabel->setText(tr("Zoom"));
}

void ImageSheet::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange) {
Expand Down Expand Up @@ -162,36 +165,20 @@ void ImageSheet::resizeEvent(QResizeEvent *event)
rubberBand->setGeometry(geometry());
}

// ZoomGroup

ZoomGroup::ZoomGroup(QWidget *parent) : QWidget(parent)
void ImageSheet::setImage(const QPixmap &image)
{
QToolButton *btnZoomIn = new QToolButton(this);
QToolButton *btnZoomOut = new QToolButton(this);
QToolButton *btnZoomNormal = new QToolButton(this);
btnZoomIn->setIcon(QIcon::fromTheme("zoom-in"));
btnZoomOut->setIcon(QIcon::fromTheme("zoom-out"));
btnZoomNormal->setIcon(QIcon::fromTheme("zoom-normal"));

labelZoom = new QLabel(this);

QHBoxLayout *layoutZoom = new QHBoxLayout(this);
layoutZoom->setMargin(0);
layoutZoom->addWidget(btnZoomIn);
layoutZoom->addWidget(btnZoomNormal);
layoutZoom->addWidget(btnZoomOut);
layoutZoom->addWidget(labelZoom);
layoutZoom->addStretch();

connect(btnZoomIn, &QToolButton::clicked, this, &ZoomGroup::zoomIn);
connect(btnZoomOut, &QToolButton::clicked, this, &ZoomGroup::zoomOut);
connect(btnZoomNormal, &QToolButton::clicked, this, &ZoomGroup::zoomReset);
scene->clear();
view->zoomReset();
view->setSceneRect(0, 0, image.width(), image.height());
pixmapItem = scene->addPixmap(image);
pixmapItem->setTransformationMode(Qt::SmoothTransformation);
sizeValueLabel->setText(QString("%1x%2").arg(image.width()).arg(image.height()));
}

void ZoomGroup::setZoomInfo(qreal factor)
void ImageSheet::retranslate()
{
const int percentage = static_cast<int>(factor * 100);
labelZoom->setText(QString("%1%").arg(percentage));
sizeLabel->setText(tr("Size"));
zoomLabel->setText(tr("Zoom"));
}

// GraphicsView
Expand Down
24 changes: 1 addition & 23 deletions src/sheets/imagesheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,6 @@ class GraphicsView : public QGraphicsView
const qreal zoomDelta = 1.25;
};

// ZoomGroup

class ZoomGroup : public QWidget
{
Q_OBJECT

public:
explicit ZoomGroup(QWidget *parent = nullptr);
void setZoomInfo(qreal factor);

signals:
void zoomIn();
void zoomOut();
void zoomReset();

private:
QLabel *labelZoom;
};

// ImageSheet

class ImageSheet : public BaseFileSheet
Expand All @@ -72,9 +53,6 @@ class ImageSheet : public BaseFileSheet

private:
void setImage(const QPixmap &image);
void setSizeInfo(int width, int height);
void setSizeInfo(const QSize &size);

void retranslate();

GraphicsView *view;
Expand All @@ -83,7 +61,7 @@ class ImageSheet : public BaseFileSheet
QLabel *sizeLabel;
QLabel *sizeValueLabel;
QLabel *zoomLabel;
ZoomGroup *zoomGroup;
QLabel *zoomValueLabel;
QRubberBand *rubberBand;
};

Expand Down

0 comments on commit f09fde9

Please sign in to comment.