Skip to content

Commit

Permalink
Merge pull request #556 from Tarsnap/ttabwidget
Browse files Browse the repository at this point in the history
TTabWidget
  • Loading branch information
gperciva authored Apr 25, 2022
2 parents f9e85fb + ba7f8f9 commit b155a35
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 45 deletions.
2 changes: 2 additions & 0 deletions Tarsnap.pro
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SOURCES += \
lib/widgets/TPathComboBrowse.cpp \
lib/widgets/TPathLineBrowse.cpp \
lib/widgets/TPopupPushButton.cpp \
lib/widgets/TTabWidget.cpp \
lib/widgets/TTextView.cpp \
lib/widgets/TWizard.cpp \
lib/widgets/TWizardPage.cpp \
Expand Down Expand Up @@ -116,6 +117,7 @@ HEADERS += \
lib/widgets/TPathComboBrowse.h \
lib/widgets/TPathLineBrowse.h \
lib/widgets/TPopupPushButton.h \
lib/widgets/TTabWidget.h \
lib/widgets/TTextView.h \
lib/widgets/TWizard.h \
lib/widgets/TWizardPage.h \
Expand Down
14 changes: 13 additions & 1 deletion forms/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<widget class="QTabWidget" name="mainTabWidget">
<widget class="TTabWidget" name="mainTabWidget">
<property name="minimumSize">
<size>
<width>0</width>
Expand Down Expand Up @@ -103,6 +103,12 @@ border-radius: 2px;
<property name="currentIndex">
<number>0</number>
</property>
<property name="largeLogoFilename">
<string notr="true">:/logos/tarsnap-header-h29.png</string>
</property>
<property name="smallLogoFilename">
<string notr="true">:/logos/tarsnap-icon-h29.png</string>
</property>
<widget class="BackupTabWidget" name="backupTabWidget">
<property name="acceptDrops">
<bool>false</bool>
Expand Down Expand Up @@ -692,6 +698,12 @@ font-size: 12px;
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>TTabWidget</class>
<extends>QTabWidget</extends>
<header>TTabWidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>TTextView</class>
<extends>QPlainTextEdit</extends>
Expand Down
47 changes: 47 additions & 0 deletions lib/plugins/TTabWidgetPlugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "TTabWidgetPlugin.h"

#include "TTabWidget.h"

TTabWidgetPlugin::TTabWidgetPlugin(QObject *parent) : QObject(parent)
{
}

QIcon TTabWidgetPlugin::icon() const
{
return QIcon();
}

QString TTabWidgetPlugin::group() const
{
return QStringLiteral("Containers");
}

QString TTabWidgetPlugin::includeFile() const
{
return QStringLiteral("TTabWidget.h");
}

QString TTabWidgetPlugin::name() const
{
return QStringLiteral("TTabWidget");
}

QString TTabWidgetPlugin::toolTip() const
{
return QString();
}

QString TTabWidgetPlugin::whatsThis() const
{
return QString();
}

QWidget *TTabWidgetPlugin::createWidget(QWidget *parent)
{
return new TTabWidget(parent);
}

bool TTabWidgetPlugin::isContainer() const
{
return false;
}
32 changes: 32 additions & 0 deletions lib/plugins/TTabWidgetPlugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef TTABWIGETPLUGIN_H
#define TTABWIGETPLUGIN_H

#include "warnings-disable.h"

WARNINGS_DISABLE
#include <QObject>
#include <QtUiPlugin/QDesignerCustomWidgetInterface>
WARNINGS_ENABLE

class TTabWidgetPlugin : public QObject, public QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_INTERFACES(QDesignerCustomWidgetInterface)

public:
explicit TTabWidgetPlugin(QObject *parent = nullptr);

QIcon icon() const override;
QString group() const override;
QString includeFile() const override;
QString name() const override;
QString toolTip() const override;
QString whatsThis() const override;
QWidget *createWidget(QWidget *parent) override;
bool isContainer() const override;

private:
bool initialized = false;
};

#endif /* !TTABWIGETPLUGIN_H */
2 changes: 2 additions & 0 deletions lib/plugins/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "TPathComboBrowsePlugin.h"
#include "TPathLineBrowsePlugin.h"
#include "TPopupPushButtonPlugin.h"
#include "TTabWidgetPlugin.h"
#include "TTextViewPlugin.h"
#include "TWizardPagePlugin.h"

Expand All @@ -17,6 +18,7 @@ TarsnapPlugins::TarsnapPlugins(QObject *parent) : QObject(parent)
widgets.append(new TPathComboBrowsePlugin(this));
widgets.append(new TPathLineBrowsePlugin(this));
widgets.append(new TPopupPushButtonPlugin(this));
widgets.append(new TTabWidgetPlugin(this));
widgets.append(new TTextViewPlugin(this));
widgets.append(new TWizardPagePlugin(this));
}
Expand Down
4 changes: 4 additions & 0 deletions lib/plugins/plugins.pro
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ HEADERS = plugins.h \
../widgets/TPathComboBrowse.h \
../widgets/TPathLineBrowse.h \
../widgets/TPopupPushButton.h \
../widgets/TTabWidget.h \
../widgets/TTextView.h \
../widgets/TWizardPage.h \
TBusyLabelPlugin.h \
Expand All @@ -19,6 +20,7 @@ HEADERS = plugins.h \
TPathComboBrowsePlugin.h \
TPathLineBrowsePlugin.h \
TPopupPushButtonPlugin.h \
TTabWidgetPlugin.h \
TTextViewPlugin.h \
TWizardPagePlugin.h

Expand All @@ -29,6 +31,7 @@ SOURCES = plugins.cpp \
../widgets/TPathComboBrowse.cpp \
../widgets/TPathLineBrowse.cpp \
../widgets/TPopupPushButton.cpp \
../widgets/TTabWidget.cpp \
../widgets/TTextView.cpp \
../widgets/TWizardPage.cpp \
TBusyLabelPlugin.cpp \
Expand All @@ -37,6 +40,7 @@ SOURCES = plugins.cpp \
TPathComboBrowsePlugin.cpp \
TPathLineBrowsePlugin.cpp \
TPopupPushButtonPlugin.cpp \
TTabWidgetPlugin.cpp \
TTextViewPlugin.cpp \
TWizardPagePlugin.cpp

Expand Down
108 changes: 108 additions & 0 deletions lib/widgets/TTabWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "TTabWidget.h"

WARNINGS_DISABLE
#include <QPainter>
#include <QPixmap>
#include <QTabBar>
#include <QTabWidget>
WARNINGS_ENABLE

class QPaintEvent;
class QResizeEvent;

#define RIGHT_PADDING 3

TTabWidget::TTabWidget(QWidget *parent)
: QTabWidget(parent),
_needRecalculate(true),
_image_x(0),
_largeLogo(nullptr),
_smallLogo(nullptr),
_image(nullptr)
{
}

TTabWidget::~TTabWidget()
{
delete _largeLogo;
delete _smallLogo;
}

void TTabWidget::setLargeLogoFilename(const QString &largeLogoFilename)
{
// Check that we haven't used this before.
Q_ASSERT(_largeLogo == nullptr);

// Save filename and load logo.
_largeLogoFilename = largeLogoFilename;
_largeLogo = new QPixmap(_largeLogoFilename);
_needRecalculate = true;
}

void TTabWidget::setSmallLogoFilename(const QString &smallLogoFilename)
{
// Check that we haven't used this before.
Q_ASSERT(_smallLogo == nullptr);

// Save filename and load logo.
_smallLogoFilename = smallLogoFilename;
_smallLogo = new QPixmap(_smallLogoFilename);
_needRecalculate = true;
}

void TTabWidget::resizeEvent(QResizeEvent *event)
{
QTabWidget::resizeEvent(event);

// We need to recalculate the widths.
//
// If this was a perfectly reusable class, we would also recalculate after
// every ::tabInserted() and ::tabRemoved(). However, this is sufficient
// for Tarsnap-GUI.
_needRecalculate = true;
}

void TTabWidget::paintEvent(QPaintEvent *event)
{
(void)event; /* UNUSED */

// Update width calculation if necessary.
if(_needRecalculate)
recalculateWidth();

// Bail if it's too narrow to draw either logo.
if(_image == nullptr)
return;

// Draw the selected logo.
QPainter p(this);
p.drawPixmap(_image_x, 0, *_image);
}

void TTabWidget::recalculateWidth()
{
// We don't need to call this again (unless something else changes).
_needRecalculate = true;

// Bail if we're missing either logo.
if((!_largeLogo) || (!_smallLogo))
return;

// How much width is available?
int remainingWidth = width() - tabBar()->width() - RIGHT_PADDING;

// Pick which image (if any) to use.
if(remainingWidth > _largeLogo->width())
_image = _largeLogo;
else if(remainingWidth > _smallLogo->width())
_image = _smallLogo;
else
{
_image = nullptr;
// It doesn't matter what _image_x is in this case.
return;
}

// How far along (width-wise) should we draw the logo?
_image_x = width() - RIGHT_PADDING - _image->width();
}
69 changes: 69 additions & 0 deletions lib/widgets/TTabWidget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef TTABWIDGET_H
#define TTABWIDGET_H

#include "warnings-disable.h"

WARNINGS_DISABLE
#include <QObject>
#include <QString>
#include <QTabWidget>
WARNINGS_ENABLE

/* Forward declaration(s). */
class QPaintEvent;
class QPixmap;
class QResizeEvent;
class QWidget;

/*!
* \ingroup lib-widgets
* \brief The TTabWidget widget is a QTabWidget which will display one of two
* images in the top-right corner.
*
* Both largeLogoFilename and smallLogoFilename must be set exactly once.
* They are essentially arguments to the constructor, but using an indirect
* route so that TTabWidget can be used in the Qt Designer.
*/
class TTabWidget : public QTabWidget
{
Q_OBJECT

//! Filename of the large icon. Can only be set once.
Q_PROPERTY(QString largeLogoFilename MEMBER _largeLogoFilename WRITE
setLargeLogoFilename DESIGNABLE true)
//! Filename of the small icon. Can only be set once.
Q_PROPERTY(QString smallLogoFilename MEMBER _smallLogoFilename WRITE
setSmallLogoFilename DESIGNABLE true)

public:
//! Constructor.
explicit TTabWidget(QWidget *parent = nullptr);
~TTabWidget() override;

//! Set the filename of the large icon.
void setLargeLogoFilename(const QString &largeLogoFilename);
//! Set the filename of the small icon.
void setSmallLogoFilename(const QString &smallLogoFilename);

protected:
//! We need to recalculate the available width.
void resizeEvent(QResizeEvent *event) override;
//! Draw one of the logos in the top-right corner.
void paintEvent(QPaintEvent *event) override;

private:
bool _needRecalculate;
int _image_x;

// These are only here so that we can set them in the designer
QString _largeLogoFilename;
QString _smallLogoFilename;

QPixmap *_largeLogo;
QPixmap *_smallLogo;
QPixmap *_image; // Always a pointer to an existing object (or nullptr).

void recalculateWidth();
};

#endif // TTABWIDGET_H
Loading

0 comments on commit b155a35

Please sign in to comment.