InsertTableWidget support for dark mode and improved resizing

Fix for bug #12438.
This commit is contained in:
Daniel Ramoeller 2021-11-22 10:15:38 +01:00 committed by Juergen Spitzmueller
parent a9eb3c9990
commit e522c5444e
2 changed files with 22 additions and 13 deletions

View File

@ -33,7 +33,7 @@ namespace lyx {
namespace frontend { namespace frontend {
InsertTableWidget::InsertTableWidget(QWidget * parent) InsertTableWidget::InsertTableWidget(QWidget * parent)
: QWidget(parent, Qt::Popup), colwidth_(20), rowheight_(12) : QWidget(parent, Qt::Popup), colwidth_(15), rowheight_(15), minrows_(5), mincols_(5)
{ {
init(); init();
setMouseTracking(true); setMouseTracking(true);
@ -42,8 +42,8 @@ InsertTableWidget::InsertTableWidget(QWidget * parent)
void InsertTableWidget::init() void InsertTableWidget::init()
{ {
rows_ = 5; rows_ = minrows_;
cols_ = 5; cols_ = mincols_;
bottom_ = 0; bottom_ = 0;
right_ = 0; right_ = 0;
underMouse_ = false; underMouse_ = false;
@ -98,13 +98,15 @@ void InsertTableWidget::mouseMoveEvent(QMouseEvent * event)
bottom_ = event->y() / rowheight_ + 1; bottom_ = event->y() / rowheight_ + 1;
#endif #endif
if (bottom_ == rows_) { int const newrows = std::max(minrows_, bottom_ + 1);
++rows_; if (rows_ != newrows) {
rows_ = newrows;
resetGeometry(); resetGeometry();
} }
if (right_ == cols_) { int const newcols = std::max(mincols_, right_ + 1);
++cols_; if (cols_ != newcols) {
cols_ = newcols;
resetGeometry(); resetGeometry();
} }
@ -140,17 +142,20 @@ void InsertTableWidget::mousePressEvent(QMouseEvent * /*event*/)
void InsertTableWidget::paintEvent(QPaintEvent * /*event*/) void InsertTableWidget::paintEvent(QPaintEvent * /*event*/)
{ {
drawGrid(rows_, cols_, Qt::white); QPalette const palette = this->palette();
drawGrid(rows_, cols_, palette.base(), palette.text().color());
if (underMouse_) if (underMouse_)
drawGrid(bottom_, right_, Qt::darkBlue); drawGrid(bottom_, right_, palette.highlight(),
palette.highlightedText().color());
} }
void InsertTableWidget::drawGrid(int const rows, int const cols, Qt::GlobalColor const color) void InsertTableWidget::drawGrid(int const rows, int const cols,
QBrush const fillBrush, QColor lineColor)
{ {
QPainter painter(this); QPainter painter(this);
painter.setPen(Qt::darkGray); painter.setPen(lineColor);
painter.setBrush(color); painter.setBrush(fillBrush);
for (int r = 0 ; r < rows ; ++r ) { for (int r = 0 ; r < rows ; ++r ) {
for (int c = 0 ; c < cols ; ++c ) { for (int c = 0 ; c < cols ; ++c ) {

View File

@ -48,7 +48,7 @@ private:
//! initialize parameters to default values //! initialize parameters to default values
void init(); void init();
//! draw the grid //! draw the grid
void drawGrid(int rows, int cols, Qt::GlobalColor color); void drawGrid(int rows, int cols, QBrush fillBrush, QColor lineColor);
//! colwidth in pixels //! colwidth in pixels
int colwidth_; int colwidth_;
@ -56,8 +56,12 @@ private:
int rowheight_; int rowheight_;
//! total rows //! total rows
int rows_; int rows_;
//! minimum number of rows
int minrows_;
//! total cols //! total cols
int cols_; int cols_;
//! minimum number of cols
int mincols_;
//! row of pointer //! row of pointer
int bottom_; int bottom_;
//! column of pointer //! column of pointer