Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional useful widgets to the status bar #1442

Merged
merged 8 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/app.pro
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ HEADERS += \
src/checkupdatesdialog.h \
src/presetdialog.h \
src/commandlineparser.h \
src/commandlineexporter.h
src/commandlineexporter.h \
src/statusbar.h \
src/elidedlabel.h

SOURCES += \
src/importlayersdialog.cpp \
Expand Down Expand Up @@ -122,7 +124,9 @@ SOURCES += \
src/presetdialog.cpp \
src/app_util.cpp \
src/commandlineparser.cpp \
src/commandlineexporter.cpp
src/commandlineexporter.cpp \
src/statusbar.cpp \
src/elidedlabel.cpp

FORMS += \
ui/importimageseqpreview.ui \
Expand Down
132 changes: 132 additions & 0 deletions app/src/elidedlabel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "elidedlabel.h"

#include <QPainter>
#include <QSizePolicy>
#include <QTextLayout>


ElidedLabel::ElidedLabel(QWidget *parent)
: QFrame(parent)
, elided(false)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
}

//! [0]
ElidedLabel::ElidedLabel(const QString &text, QWidget *parent) :
ElidedLabel(parent)
{
content = text;
}
//! [0]

//! [1]
void ElidedLabel::setText(const QString &newText)
{
content = newText;
update();
}
//! [1]

QSize ElidedLabel::sizeHint() const
{
return fontMetrics().size(0, content);
}

//! [2]
void ElidedLabel::paintEvent(QPaintEvent *event)
{
QFrame::paintEvent(event);

QPainter painter(this);
QFontMetrics fontMetrics = painter.fontMetrics();

bool didElide = false;
int lineSpacing = fontMetrics.lineSpacing();
int y = 0;

QTextLayout textLayout(content, painter.font());
textLayout.beginLayout();
forever {
QTextLine line = textLayout.createLine();

if (!line.isValid())
break;

line.setLineWidth(width());
int nextLineY = y + lineSpacing;

if (height() >= nextLineY + lineSpacing) {
line.draw(&painter, QPoint(0, y));
y = nextLineY;
//! [2]
//! [3]
} else {
QString lastLine = content.mid(line.textStart());
QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, width());
painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLastLine);
line = textLayout.createLine();
didElide = line.isValid();
break;
}
}
textLayout.endLayout();
//! [3]

//! [4]
if (didElide != elided) {
elided = didElide;
emit elisionChanged(didElide);
}
}
//! [4]
86 changes: 86 additions & 0 deletions app/src/elidedlabel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef ELIDEDLABEL_H
#define ELIDEDLABEL_H

#include <QFrame>
#include <QString>

//! [0]
class ElidedLabel : public QFrame
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText)
Q_PROPERTY(bool isElided READ isElided)

public:
explicit ElidedLabel(QWidget *parent = nullptr);
explicit ElidedLabel(const QString &text, QWidget *parent = nullptr);

void setText(const QString &text);
const QString & text() const { return content; }
bool isElided() const { return elided; }

virtual QSize sizeHint() const override;

protected:
void paintEvent(QPaintEvent *event) override;

signals:
void elisionChanged(bool elided);

private:
bool elided;
QString content;
};
//! [0]

#endif // TEXTWRAPPINGWIDGET_H
28 changes: 18 additions & 10 deletions app/src/mainwindow2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ GNU General Public License for more details.
#include <QTabletEvent>
#include <QStandardPaths>
#include <QDateTime>
#include <QLabel>

// core_lib headers
#include "pencildef.h"
Expand Down Expand Up @@ -110,6 +109,10 @@ MainWindow2::MainWindow2(QWidget* parent) :
ui->scribbleArea->setEditor(mEditor);
ui->scribbleArea->init();

ui->statusBar->setEditor(mEditor);
ui->statusBar->updateZoomStatus();
ui->statusBar->setVisible(mEditor->preference()->isOn(SETTING::SHOW_STATUS_BAR));

mCommands = new ActionCommands(this);
mCommands->setCore(mEditor);

Expand All @@ -119,15 +122,10 @@ MainWindow2::MainWindow2(QWidget* parent) :

readSettings();

mZoomLabel = new QLabel("");
ui->statusbar->addWidget(mZoomLabel);

updateZoomLabel();
selectionChanged();

connect(mEditor, &Editor::needSave, this, &MainWindow2::autoSave);
connect(mToolBox, &ToolBoxWidget::clearButtonClicked, mEditor, &Editor::clearCurrentFrame);
connect(mEditor->view(), &ViewManager::viewChanged, this, &MainWindow2::updateZoomLabel);

mEditor->tools()->setDefaultTool();
ui->background->init(mEditor->preference());
Expand Down Expand Up @@ -226,6 +224,7 @@ void MainWindow2::createDockWidgets()
makeConnections(mEditor, mColorPalette);
makeConnections(mEditor, mToolOptions);
makeConnections(mEditor, mDisplayOptionWidget);
makeConnections(mEditor, ui->statusBar);

for (BaseDockWidget* w : mDockWidgets)
{
Expand Down Expand Up @@ -326,6 +325,8 @@ void MainWindow2::createMenus()
connect(mEditor->view(), &ViewManager::viewFlipped, this, &MainWindow2::viewFlipped);

PreferenceManager* prefs = mEditor->preference();
connect(ui->actionStatusBar, &QAction::triggered, ui->statusBar, &QStatusBar::setVisible);
bindPreferenceSetting(ui->actionStatusBar, prefs, SETTING::SHOW_STATUS_BAR);
bindPreferenceSetting(ui->actionGrid, prefs, SETTING::GRID);
bindPreferenceSetting(ui->actionOnionPrev, prefs, SETTING::PREV_ONION);
bindPreferenceSetting(ui->actionOnionNext, prefs, SETTING::NEXT_ONION);
Expand Down Expand Up @@ -423,7 +424,9 @@ void MainWindow2::setOpacity(int opacity)

void MainWindow2::updateSaveState()
{
setWindowModified(mEditor->currentBackup() != mBackupAtSave);
const bool hasUnsavedChanges = mEditor->currentBackup() != mBackupAtSave;
setWindowModified(hasUnsavedChanges);
ui->statusBar->updateModifiedStatus(hasUnsavedChanges);
}

void MainWindow2::clearRecentFilesList()
Expand Down Expand Up @@ -617,6 +620,7 @@ bool MainWindow2::openObject(const QString& strFilePath)

setWindowTitle(mEditor->object()->filePath().prepend("[*]"));
setWindowModified(false);
ui->statusBar->updateModifiedStatus(false);

progress.setValue(progress.maximum());

Expand Down Expand Up @@ -1180,6 +1184,7 @@ void MainWindow2::setupKeyboardShortcuts()
ui->actionGrid->setShortcut(cmdKeySeq(CMD_GRID));
ui->actionOnionPrev->setShortcut(cmdKeySeq(CMD_ONIONSKIN_PREV));
ui->actionOnionNext->setShortcut(cmdKeySeq(CMD_ONIONSKIN_NEXT));
ui->actionStatusBar->setShortcut(cmdKeySeq(CMD_TOGGLE_STATUS_BAR));

ui->actionPlay->setShortcut(cmdKeySeq(CMD_PLAY));
ui->actionLoop->setShortcut(cmdKeySeq(CMD_LOOP));
Expand Down Expand Up @@ -1437,10 +1442,13 @@ void MainWindow2::makeConnections(Editor* pEditor, ColorPaletteWidget* pColorPal
connect(pColorManager, &ColorManager::colorNumberChanged, pColorPalette, &ColorPaletteWidget::selectColorNumber);
}

void MainWindow2::updateZoomLabel()
void MainWindow2::makeConnections(Editor* editor, StatusBar *statusBar)
{
qreal zoom = mEditor->view()->scaling() * 100.f;
mZoomLabel->setText(tr("Zoom: %0%").arg(zoom, 0, 'f', 1));
connect(editor->tools(), &ToolManager::toolChanged, statusBar, &StatusBar::updateToolStatus);
connect(editor->tools()->getTool(POLYLINE), &BaseTool::isActiveChanged, statusBar, &StatusBar::updateToolStatus);

connect(editor->view(), &ViewManager::viewChanged, statusBar, &StatusBar::updateZoomStatus);
connect(statusBar, &StatusBar::zoomChanged, editor->view(), &ViewManager::scale);
}

void MainWindow2::changePlayState(bool isPlaying)
Expand Down
7 changes: 2 additions & 5 deletions app/src/mainwindow2.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class ActionCommands;
class ImportImageSeqDialog;
class BackupElement;
class LayerOpacityDialog;
class QLabel;
class PegBarAlignmentDialog;
class StatusBar;
enum class SETTING;


Expand Down Expand Up @@ -121,7 +121,6 @@ public slots:
void createMenus();
void setupKeyboardShortcuts();
void clearKeyboardShortcuts();
void updateZoomLabel();
bool loadMostRecent();
bool tryLoadPreset();

Expand All @@ -144,6 +143,7 @@ public slots:
void makeConnections(Editor*, DisplayOptionWidget*);
void makeConnections(Editor*, ToolOptionWidget*);
void makeConnections(Editor*, OnionSkinWidget*);
void makeConnections(Editor*, StatusBar*);

bool tryRecoverUnsavedProject();
void startProjectRecovery(int result);
Expand Down Expand Up @@ -177,9 +177,6 @@ public slots:
// a hack for MacOS because closeEvent fires twice
bool m2ndCloseEvent = false;

// statusbar widgets
QLabel* mZoomLabel = nullptr;

// Whether to suppress the auto save dialog due to internal work
bool mSuppressAutoSaveDialog = false;

Expand Down
1 change: 1 addition & 0 deletions app/src/shortcutspage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ static QString getHumanReadableShortcutName(const QString& cmdName)
{CMD_SAVE_AS, ShortcutsPage::tr("Save File As", "Shortcut")},
{CMD_SAVE_FILE, ShortcutsPage::tr("Save File", "Shortcut")},
{CMD_SELECT_ALL, ShortcutsPage::tr("Select All", "Shortcut")},
{CMD_TOGGLE_STATUS_BAR, ShortcutsPage::tr("Toggle Status Bar Visibility", "Shortcut")},
{CMD_TOGGLE_COLOR_INSPECTOR, ShortcutsPage::tr("Toggle Color Inspector Window Visibility", "Shortcut")},
{CMD_TOGGLE_COLOR_LIBRARY, ShortcutsPage::tr("Toggle Color Palette Window Visibility", "Shortcut")},
{CMD_TOGGLE_COLOR_WHEEL, ShortcutsPage::tr("Toggle Color Box Window Visibility", "Shortcut")},
Expand Down
Loading