refine VSelectDialog with QListWidget

This commit is contained in:
Le Tan 2018-03-06 19:33:00 +08:00
parent 1b1c530392
commit ccd3c55cad
5 changed files with 212 additions and 32 deletions

View File

@ -1,5 +1,9 @@
#include <QtWidgets>
#include "vselectdialog.h"
#include "utils/vimnavigationforwidget.h"
#define CANCEL_ID -1
VSelectDialog::VSelectDialog(const QString &p_title, QWidget *p_parent)
: QDialog(p_parent), m_choice(-1)
@ -9,16 +13,26 @@ VSelectDialog::VSelectDialog(const QString &p_title, QWidget *p_parent)
void VSelectDialog::setupUI(const QString &p_title)
{
m_mainLayout = new QVBoxLayout();
m_list = new QListWidget();
m_list->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_list->setSelectionMode(QAbstractItemView::SingleSelection);
m_list->setAttribute(Qt::WA_MacShowFocusRect, false);
connect(m_list, &QListWidget::itemActivated,
this, &VSelectDialog::selectionChosen);
QPushButton *cancelBtn = new QPushButton(tr("Cancel"));
cancelBtn->setProperty("SelectionBtn", true);
connect(cancelBtn, &QPushButton::clicked,
this, &VSelectDialog::reject);
m_mainLayout->addWidget(cancelBtn);
// Add cancel item.
QListWidgetItem *cancelItem = new QListWidgetItem(tr("Cancel"));
cancelItem->setData(Qt::UserRole, CANCEL_ID);
setLayout(m_mainLayout);
m_mainLayout->setSizeConstraint(QLayout::SetFixedSize);
m_list->addItem(cancelItem);
m_list->setCurrentRow(0);
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(m_list);
layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout);
setWindowTitle(p_title);
}
@ -26,29 +40,58 @@ void VSelectDialog::addSelection(const QString &p_selectStr, int p_selectID)
{
Q_ASSERT(p_selectID >= 0);
QPushButton *btn = new QPushButton(p_selectStr);
btn->setProperty("SelectionBtn", true);
if (m_selections.isEmpty()) {
btn->setDefault(true);
btn->setAutoDefault(true);
}
connect(btn, &QPushButton::clicked,
this, &VSelectDialog::selectionChosen);
m_selections.insert(btn, p_selectID);
m_mainLayout->insertWidget(m_selections.size() - 1, btn);
QListWidgetItem *item = new QListWidgetItem(p_selectStr);
item->setData(Qt::UserRole, p_selectID);
m_list->insertItem(m_list->count() - 1, item);
m_list->setCurrentRow(0);
}
void VSelectDialog::selectionChosen()
void VSelectDialog::selectionChosen(QListWidgetItem *p_item)
{
QPushButton *btn = dynamic_cast<QPushButton *>(sender());
Q_ASSERT(btn);
auto it = m_selections.find(btn);
Q_ASSERT(it != m_selections.end());
m_choice = *it;
accept();
m_choice = p_item->data(Qt::UserRole).toInt();
if (m_choice == CANCEL_ID) {
reject();
} else {
accept();
}
}
int VSelectDialog::getSelection() const
{
return m_choice;
}
void VSelectDialog::updateSize()
{
Q_ASSERT(m_list->count() > 0);
int height = 0;
for (int i = 0; i < m_list->count(); ++i) {
height += m_list->sizeHintForRow(i);
}
height += 2 * m_list->count();
int wid = width();
m_list->resize(wid, height);
resize(wid, height);
}
void VSelectDialog::showEvent(QShowEvent *p_event)
{
QDialog::showEvent(p_event);
updateSize();
}
void VSelectDialog::keyPressEvent(QKeyEvent *p_event)
{
if (VimNavigationForWidget::injectKeyPressEventForVim(m_list,
p_event,
this)) {
return;
}
QDialog::keyPressEvent(p_event);
}

View File

@ -5,8 +5,11 @@
#include <QMap>
class QPushButton;
class QVBoxLayout;
class QMouseEvent;
class QListWidget;
class QListWidgetItem;
class QShowEvent;
class QKeyEvent;
class VSelectDialog : public QDialog
{
@ -14,18 +17,27 @@ class VSelectDialog : public QDialog
public:
VSelectDialog(const QString &p_title, QWidget *p_parent = 0);
// @p_selectID should >= 0.
void addSelection(const QString &p_selectStr, int p_selectID);
int getSelection() const;
protected:
void showEvent(QShowEvent *p_event) Q_DECL_OVERRIDE;
void keyPressEvent(QKeyEvent *p_event) Q_DECL_OVERRIDE;
private slots:
void selectionChosen();
void selectionChosen(QListWidgetItem *p_item);
private:
void setupUI(const QString &p_title);
QVBoxLayout *m_mainLayout;
void updateSize();
int m_choice;
QMap<QPushButton *, int> m_selections;
QListWidget *m_list;
};
#endif // VSELECTDIALOG_H

View File

@ -216,7 +216,7 @@ QPushButton:flat {
}
QPushButton:default {
border-color: @pushbutton_default_border;
border: 1px solid @pushbutton_default_border;
}
QPushButton:disabled {
@ -260,6 +260,11 @@ QPushButton[SpecialBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[SpecialBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: @pushbutton_specialbtn_bg;
}
QPushButton[CornerBtn="true"] {
padding: 4px -2px 4px -2px;
margin: 0px;
@ -289,6 +294,11 @@ QPushButton[CornerBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[CornerBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
QPushButton[StatusBtn="true"] {
font: bold;
padding: 0px 2px 0px 2px;
@ -315,6 +325,11 @@ QPushButton[StatusBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[StatusBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
QPushButton[FlatBtn="true"] {
padding: 4px;
margin: 0px;
@ -340,6 +355,11 @@ QPushButton[FlatBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[FlatBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
QPushButton[SelectionBtn="true"] {
padding: 4px 10px 4px 10px;
border: none;
@ -366,6 +386,11 @@ QPushButton[SelectionBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[SelectionBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
QPushButton[TitleBtn="true"] {
padding: 4px;
margin: 0px;
@ -391,6 +416,11 @@ QPushButton[TitleBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[TitleBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: @pushbutton_titlebtn_bg;
}
QPushButton[DangerBtn="true"] {
color: @pushbutton_dangerbtn_fg;
border: none;
@ -415,6 +445,11 @@ QPushButton[DangerBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[DangerBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: @pushbutton_dangerbtn_bg;
}
QPushButton[ToolBoxActiveBtn="true"] {
padding: 4px 10px 4px 4px;
margin: 0px;
@ -441,6 +476,11 @@ QPushButton[ToolBoxActiveBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[ToolBoxActiveBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: @pushbutton_toolboxbtn_active_bg;
}
QPushButton[AvatarBtn="true"] {
padding: 2px 4px 2px 4px;
margin: 0px;
@ -466,6 +506,11 @@ QPushButton[AvatarBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[AvatarBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
VButtonMenuItem {
padding: 5px;
padding-right: 30px;

View File

@ -216,7 +216,7 @@ QPushButton:flat {
}
QPushButton:default {
border-color: @pushbutton_default_border;
border: 1px solid @pushbutton_default_border;
}
QPushButton:disabled {
@ -260,6 +260,11 @@ QPushButton[SpecialBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[SpecialBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: @pushbutton_specialbtn_bg;
}
QPushButton[CornerBtn="true"] {
padding: 4px -2px 4px -2px;
margin: 0px;
@ -289,6 +294,11 @@ QPushButton[CornerBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[CornerBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
QPushButton[StatusBtn="true"] {
font: bold;
padding: 0px 2px 0px 2px;
@ -315,6 +325,11 @@ QPushButton[StatusBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[StatusBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
QPushButton[FlatBtn="true"] {
padding: 4px;
margin: 0px;
@ -340,6 +355,11 @@ QPushButton[FlatBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[FlatBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
QPushButton[SelectionBtn="true"] {
padding: 4px 10px 4px 10px;
border: none;
@ -366,6 +386,11 @@ QPushButton[SelectionBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[SelectionBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
QPushButton[TitleBtn="true"] {
padding: 4px;
margin: 0px;
@ -391,6 +416,11 @@ QPushButton[TitleBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[TitleBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: @pushbutton_titlebtn_bg;
}
QPushButton[DangerBtn="true"] {
color: @pushbutton_dangerbtn_fg;
border: none;
@ -410,6 +440,11 @@ QPushButton[DangerBtn="true"]:pressed {
background-color: @pushbutton_dangerbtn_pressed_bg;
}
QPushButton[DangerBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: @pushbutton_dangerbtn_bg;
}
QPushButton[DangerBtn="true"]:disabled {
color: @pushbutton_disabled_fg;
background-color: @pushbutton_disabled_bg;
@ -441,6 +476,11 @@ QPushButton[ToolBoxActiveBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[ToolBoxActiveBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: @pushbutton_toolboxbtn_active_bg;
}
QPushButton[AvatarBtn="true"] {
padding: 2px 4px 2px 4px;
margin: 0px;
@ -466,6 +506,11 @@ QPushButton[AvatarBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[AvatarBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
VButtonMenuItem {
padding: 5px;
padding-right: 30px;

View File

@ -204,7 +204,7 @@ QPushButton:flat {
}
QPushButton:default {
border-color: @pushbutton_default_border;
border: 1px solid @pushbutton_default_border;
}
QPushButton:disabled {
@ -247,6 +247,11 @@ QPushButton[CornerBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[CornerBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
QPushButton[StatusBtn="true"] {
font: bold;
padding: 0px 2px 0px 2px;
@ -273,6 +278,11 @@ QPushButton[StatusBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[StatusBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
QPushButton[FlatBtn="true"] {
padding: 4px;
margin: 0px;
@ -298,6 +308,11 @@ QPushButton[FlatBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[FlatBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
QPushButton[SelectionBtn="true"] {
padding: 4px 10px 4px 10px;
border: none;
@ -324,6 +339,11 @@ QPushButton[SelectionBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[SelectionBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
QPushButton[TitleBtn="true"] {
padding: 4px;
margin: 0px;
@ -349,6 +369,11 @@ QPushButton[TitleBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[TitleBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: @pushbutton_titlebtn_bg;
}
QPushButton[DangerBtn="true"] {
color: @pushbutton_dangerbtn_fg;
border: none;
@ -373,6 +398,11 @@ QPushButton[DangerBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[DangerBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: @pushbutton_dangerbtn_bg;
}
QPushButton[AvatarBtn="true"] {
padding: 2px 4px 2px 4px;
margin: 0px;
@ -398,6 +428,11 @@ QPushButton[AvatarBtn="true"]:disabled {
background-color: @pushbutton_disabled_bg;
}
QPushButton[AvatarBtn="true"]:default {
border: 1px solid @pushbutton_default_border;
background-color: transparent;
}
VButtonMenuItem {
padding: 5px;
padding-right: 30px;