-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #556 from Tarsnap/ttabwidget
TTabWidget
- Loading branch information
Showing
13 changed files
with
296 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.