AppSwitcher: set orientation
Layout now adjusts to specified orientation
This commit is contained in:
parent
1b2d66990d
commit
adb52cde3f
|
@ -42,6 +42,11 @@ void AppItemDelegate::init()
|
|||
m_maxTextWidth = Settings::instance().maxTextWidth();
|
||||
}
|
||||
|
||||
void AppItemDelegate::setOrientation(Qt::Orientation orient)
|
||||
{
|
||||
m_orientation = orient;
|
||||
}
|
||||
|
||||
void AppItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
{
|
||||
QStyle* style = option.widget ? option.widget->style() : QApplication::style();
|
||||
|
@ -49,7 +54,19 @@ void AppItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
|
|||
|
||||
QPixmap img = index.model()->data(index, AppRole::Icon).value<QPixmap>();
|
||||
|
||||
painter->drawPixmap(option.rect.left() + 2, option.rect.top() + 2, m_iconSize, m_iconSize, img);
|
||||
option.rect.left() + (option.rect.width() - m_iconSize) / 2;
|
||||
|
||||
if(m_orientation == Qt::Horizontal)
|
||||
{
|
||||
// Vertically center icon
|
||||
painter->drawPixmap(option.rect.left() + (option.rect.width() - m_iconSize) / 2,
|
||||
option.rect.top() + 2, m_iconSize, m_iconSize, img);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Draw icon on the left
|
||||
painter->drawPixmap(option.rect.left() + 2, option.rect.top() + 2, m_iconSize, m_iconSize, img);
|
||||
}
|
||||
|
||||
QString text = index.model()->data(index, AppRole::Display).toString();
|
||||
if (text.length() > m_maxTextWidth) {
|
||||
|
@ -58,9 +75,20 @@ void AppItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
|
|||
|
||||
painter->setFont(option.font);
|
||||
|
||||
painter->drawText(QRectF(option.rect.left() + m_iconSize + 5, option.rect.top() + 2,
|
||||
option.rect.width() - m_iconSize - 5, m_iconSize),
|
||||
Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, text);
|
||||
if(m_orientation == Qt::Horizontal)
|
||||
{
|
||||
// Put text under icon
|
||||
painter->drawText(QRectF(option.rect.left() + 3, option.rect.top() + m_iconSize + 5,
|
||||
option.rect.width() - 3, option.rect.height() - m_iconSize - 5),
|
||||
Qt::AlignLeft | Qt::AlignVCenter | Qt::TextWordWrap, text);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Put text on the right, after icon
|
||||
painter->drawText(QRectF(option.rect.left() + m_iconSize + 5, option.rect.top() + 2,
|
||||
option.rect.width() - m_iconSize - 5, m_iconSize),
|
||||
Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, text);
|
||||
}
|
||||
}
|
||||
|
||||
QSize AppItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
|
@ -72,5 +100,14 @@ QSize AppItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel
|
|||
text = text.left(m_maxTextWidth) + "...";
|
||||
}
|
||||
|
||||
if(m_orientation == Qt::Horizontal)
|
||||
{
|
||||
// Put text under icon
|
||||
QRect possibleRect(0, 0, m_iconSize, m.height() * 2);
|
||||
QRect textRect = m.boundingRect(possibleRect, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextWordWrap, text);
|
||||
return QSize(qMax(m_iconSize + 4, textRect.width() + 6), m_iconSize + 10 + textRect.height());
|
||||
}
|
||||
|
||||
// Put text on the right, after icon
|
||||
return QSize(m_iconSize + 10 + m.horizontalAdvance(text), m_iconSize + 4);
|
||||
}
|
||||
|
|
|
@ -34,6 +34,8 @@ public:
|
|||
AppItemDelegate(QObject* parent);
|
||||
void init();
|
||||
|
||||
void setOrientation(Qt::Orientation orient);
|
||||
|
||||
protected:
|
||||
virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
|
||||
virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
|
||||
|
@ -41,4 +43,5 @@ protected:
|
|||
private:
|
||||
int m_iconSize = 32;
|
||||
int m_maxTextWidth = 0;
|
||||
Qt::Orientation m_orientation = Qt::Vertical;
|
||||
};
|
||||
|
|
|
@ -68,9 +68,11 @@ AppSwitcher::AppSwitcher(QWidget* parent)
|
|||
}
|
||||
});
|
||||
|
||||
setItemDelegate(new AppItemDelegate(this));
|
||||
m_delegate = new AppItemDelegate(this);
|
||||
setItemDelegate(m_delegate);
|
||||
setContentsMargins(5, 5, 5, 5);
|
||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
setOrientation(Qt::Vertical);
|
||||
|
||||
m_timer = new QTimer(this);
|
||||
m_timer->setInterval(100);
|
||||
m_timer->setSingleShot(true);
|
||||
|
@ -123,6 +125,24 @@ void AppSwitcher::showSwitcher(bool forward)
|
|||
m_timer->start();
|
||||
}
|
||||
|
||||
void AppSwitcher::setOrientation(Qt::Orientation orientation)
|
||||
{
|
||||
m_delegate->setOrientation(orientation);
|
||||
|
||||
if(orientation == Qt::Horizontal)
|
||||
{
|
||||
setFlow(QListView::LeftToRight);
|
||||
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
||||
}
|
||||
else
|
||||
{
|
||||
setFlow(QListView::TopToBottom);
|
||||
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
}
|
||||
}
|
||||
|
||||
void AppSwitcher::selectItem(bool forward)
|
||||
{
|
||||
m_timer->start();
|
||||
|
|
|
@ -34,6 +34,8 @@ namespace GlobalKeyShortcut {
|
|||
class Action;
|
||||
}
|
||||
|
||||
class AppItemDelegate;
|
||||
|
||||
class AppSwitcher : public QListView
|
||||
{
|
||||
Q_OBJECT
|
||||
|
@ -42,6 +44,8 @@ public:
|
|||
|
||||
void showSwitcher(bool forward = true);
|
||||
|
||||
void setOrientation(Qt::Orientation orientation);
|
||||
|
||||
protected:
|
||||
void keyReleaseEvent(QKeyEvent* event) override;
|
||||
void closeEvent(QCloseEvent*) override;
|
||||
|
@ -56,4 +60,5 @@ private:
|
|||
GlobalKeyShortcut::Action* m_globalRShortcut;
|
||||
QTimer* m_timer;
|
||||
int m_current = 0;
|
||||
AppItemDelegate* m_delegate = nullptr;
|
||||
};
|
||||
|
|
|
@ -55,8 +55,10 @@ int main(int argc, char* argv[])
|
|||
app.setStyleSheet(QLatin1String("file:///%1/lxqt-app-switcher.qss").arg(theme.path()));
|
||||
}
|
||||
|
||||
Settings::instance().setIconSize(512);
|
||||
|
||||
AppSwitcher switcher(nullptr);
|
||||
switcher.setOrientation(Qt::Horizontal);
|
||||
switcher.showSwitcher();
|
||||
|
||||
return app.exec();
|
||||
|
|
Loading…
Reference in New Issue