mirror-chatterino2/src/widgets/listview/GenericListView.cpp
fourtf 50da694fff
Stable (#1905)
* fixed SearchWindow memory leak

* Update CHANGELOG.md

* added DebugCount for BaseWindow

* [Bug Fix] Color of second user highlight cannot be set (#1898)

* Highlighting: Fix bug preventing user highlight from being set

Before this commit, only the row of a clicked cell was checked, but not
the tab it was clicked in.

Since the "Whispers" row is the second row in the "Messages" tab on the
highlighting page, the color picker was not opened for the second entry
in the "Users" tab either. This commit fixes the bug by also checking
tab the cell was clicked in.

* Update CHANGELOG.md

* Emote Popup Improvements (#1895)

* Put exact matching emotes first

* Close GenericListView on Escape press

* smol fix

* fixed emote input when not in the first char

* fixes #1902

* closes #1904

Co-authored-by: Leon Richardt <leon.richardt@gmail.com>
Co-authored-by: Daniel <24928223+dnsge@users.noreply.github.com>
2020-08-23 11:28:22 +02:00

128 lines
3.3 KiB
C++

#include "GenericListView.hpp"
#include "singletons/Theme.hpp"
#include "widgets/listview/GenericListModel.hpp"
namespace chatterino {
GenericListView::GenericListView()
: itemDelegate_(this)
{
this->setSelectionMode(QAbstractItemView::SingleSelection);
this->setSelectionBehavior(QAbstractItemView::SelectItems);
this->setItemDelegate(&this->itemDelegate_);
QObject::connect(
this, &QListView::clicked, this, [this](const QModelIndex &index) {
auto *item = GenericListItem::fromVariant(index.data());
item->action();
emit this->closeRequested();
});
}
void GenericListView::setModel(QAbstractItemModel *model)
{
auto casted = dynamic_cast<GenericListModel *>(model);
assert(casted);
this->setModel(casted);
}
void GenericListView::setModel(GenericListModel *model)
{
this->model_ = model;
QListView::setModel(model);
}
void GenericListView::setInvokeActionOnTab(bool value)
{
this->invokeActionOnTab_ = value;
}
bool GenericListView::eventFilter(QObject * /*watched*/, QEvent *event)
{
if (!this->model_)
return false;
if (event->type() == QEvent::KeyPress)
{
auto *keyEvent = static_cast<QKeyEvent *>(event);
int key = keyEvent->key();
const QModelIndex &curIdx = this->currentIndex();
const int curRow = curIdx.row();
const int count = this->model_->rowCount(curIdx);
if (key == Qt::Key_Enter || key == Qt::Key_Return ||
(key == Qt::Key_Tab && this->invokeActionOnTab_))
{
// keep this before the other tab handler
if (count <= 0)
return true;
const auto index = this->currentIndex();
auto *item = GenericListItem::fromVariant(index.data());
item->action();
emit this->closeRequested();
return true;
}
else if (key == Qt::Key_Down || key == Qt::Key_Tab)
{
if (count <= 0)
return true;
const int newRow = (curRow + 1) % count;
this->setCurrentIndex(curIdx.siblingAtRow(newRow));
return true;
}
else if (key == Qt::Key_Up ||
(!this->invokeActionOnTab_ && key == Qt::Key_Backtab))
{
if (count <= 0)
return true;
int newRow = curRow - 1;
if (newRow < 0)
newRow += count;
this->setCurrentIndex(curIdx.siblingAtRow(newRow));
return true;
}
else if (key == Qt::Key_Escape)
{
emit this->closeRequested();
return true;
}
else
{
return false;
}
}
return false;
}
void GenericListView::refreshTheme(const Theme &theme)
{
const QString textCol = theme.window.text.name();
const QString bgCol = theme.window.background.name();
const QString selCol =
(theme.isLightTheme()
? "#68B1FF" // Copied from Theme::splits.input.styleSheet
: theme.tabs.selected.backgrounds.regular.color().name());
const QString listStyle =
QString(
"color: %1; background-color: %2; selection-background-color: %3")
.arg(textCol)
.arg(bgCol)
.arg(selCol);
this->setStyleSheet(listStyle);
}
} // namespace chatterino