Skip to content

Commit

Permalink
Table headers
Browse files Browse the repository at this point in the history
Icon to the right
Text better elided
  • Loading branch information
davy7125 committed Oct 17, 2019
1 parent 903ecb8 commit 9800a43
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 18 deletions.
15 changes: 2 additions & 13 deletions sources/editor/pagetable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,6 @@ void PageTable::addGlobal(IdList listIds)
bool offsetEndLoopDefined = false;
int row;

QString qStr;
if (multiGlobal)
{
qStr = _sf2->getQstr(id, champ_name).left(10);
qStr.append("\n");
qStr.append(_sf2->getQstr(id, champ_name).mid(10).left(10));
}
else
qStr = trUtf8("Global");

// Column number
int numCol = 0;
for (int i = 0; i < nbGlobal; i++)
Expand All @@ -113,7 +103,7 @@ void PageTable::addGlobal(IdList listIds)
break;
}

_table->addColumn(numCol, qStr, id);
_table->addColumn(numCol, multiGlobal ? _sf2->getQstr(id, champ_name) : trUtf8("Global"), id);
nbGlobal++;
EltID idGen = id;
idGen.typeElement = this->contenantGen;
Expand Down Expand Up @@ -223,7 +213,6 @@ void PageTable::addDivisions(EltID id)
.arg(_sf2->get(id, champ_keyRange).rValue.byLo, 3, 10, QChar('0'))
.arg(_sf2->get(id, champ_velRange).rValue.byLo, 3, 10, QChar('0'))
.arg(_sf2->getQstr(id3, champ_name));
QString qStr = _sf2->getQstr(id3, champ_name).left(10) + "\n" + _sf2->getQstr(id3, champ_name).mid(10).left(10);
for (int j = 1; j < nbSmplInst + 1; j++)
{
if (Utils::sortDivisions(id, _table->getID(j), _sortType) > 0)
Expand All @@ -241,7 +230,7 @@ void PageTable::addDivisions(EltID id)
bool offsetStartLoopDefined = false;
int offsetEndLoop = 0;
bool offsetEndLoopDefined = false;
_table->addColumn(numCol, qStr, id);
_table->addColumn(numCol, _sf2->getQstr(id3, champ_name), id);
foreach (int champTmp, _sf2->getSiblings(id2))
{
genValTmp = _sf2->get(id, static_cast<AttributeType>(champTmp));
Expand Down
62 changes: 62 additions & 0 deletions sources/editor/widgets/tableheaderview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include <QMouseEvent>
#include <QMenu>
#include "soundfontmanager.h"
#include <QPainter>

const int TableHeaderView::MARGIN = 2;

TableHeaderView::TableHeaderView(QWidget *parent) : QHeaderView(Qt::Horizontal, parent)
{
Expand All @@ -48,6 +51,10 @@ TableHeaderView::TableHeaderView(QWidget *parent) : QHeaderView(Qt::Horizontal,

QAction * action2 = _menu->addAction(trUtf8("unmute all"));
connect(action2, SIGNAL(triggered(bool)), this, SLOT(unmuteAll(bool)));

// Height of the header
QFontMetrics fm(this->font());
_height = (fm.height() + MARGIN) * 2;
}

TableHeaderView::~TableHeaderView()
Expand Down Expand Up @@ -175,3 +182,58 @@ void TableHeaderView::unmuteAll(bool unused)
for (int i = 0; i < this->model()->columnCount(); i++)
this->model()->setHeaderData(i, Qt::Horizontal, QVariant(), Qt::DecorationRole);
}

QSize TableHeaderView::sizeHint() const
{
// Override the height with a custom value
QSize size = QHeaderView::sizeHint();
size.setHeight(_height);
return size;
}

void TableHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
// Get the text and icon to display
QString text = this->model()->headerData(logicalIndex, this->orientation(), Qt::DisplayRole).toString();
QPixmap icon = this->model()->headerData(logicalIndex, this->orientation(), Qt::DecorationRole).value<QPixmap>();

// Icon and text rect
QRect iconRect = icon.rect();
QRect textRect = rect;
textRect.setWidth(textRect.width() - 2 * MARGIN);
if (iconRect.width() > 0)
textRect.setWidth(textRect.width() - iconRect.width() - MARGIN);
textRect.translate(MARGIN, MARGIN);

// Adapt the text
QString adaptedText = text;
int lengthLine1 = text.length();
QFontMetrics fm(this->font());
while (fm.width(text.left(lengthLine1)) > textRect.width() && lengthLine1 > 0)
lengthLine1--;
if (lengthLine1 < text.length())
adaptedText = text.left(lengthLine1) + "\n" + fm.elidedText(text.mid(lengthLine1), Qt::ElideRight, textRect.width());
else
textRect.translate(0, fm.height() / 2);

// First draw the cell without text or icon for the background and border
this->model()->setHeaderData(logicalIndex, this->orientation(), "", Qt::DisplayRole);
this->model()->setHeaderData(logicalIndex, this->orientation(), QVariant(), Qt::DecorationRole);
QHeaderView::paintSection(painter, rect, logicalIndex);
this->model()->setHeaderData(logicalIndex, this->orientation(), text, Qt::DisplayRole);
this->model()->setHeaderData(logicalIndex, this->orientation(), icon, Qt::DecorationRole);

// Then draw the text
QVariant foregroundBrush = model()->headerData(logicalIndex, this->orientation(), Qt::ForegroundRole);
if (foregroundBrush.canConvert<QBrush>())
painter->setPen(foregroundBrush.value<QBrush>().color());
painter->setClipRect(rect);
painter->drawText(textRect, Qt::AlignHCenter, adaptedText);

// Finally draw the icon on the right and vertically centered
int iconHeight = iconRect.height();
int headerHeight = rect.height();
int offsetY = iconHeight < headerHeight ? (headerHeight - iconHeight) / 2 : 0;
painter->setClipRect(rect);
painter->drawPixmap(textRect.right() + MARGIN, rect.top() + offsetY, iconRect.width(), iconRect.height(), icon);
}
5 changes: 5 additions & 0 deletions sources/editor/widgets/tableheaderview.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class TableHeaderView: public QHeaderView

protected:
void mousePressEvent(QMouseEvent * e);
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;
QSize sizeHint() const;

private slots:
void mute(bool isMute);
Expand All @@ -53,6 +55,9 @@ private slots:
QAction * _muteAction;
int _currentSection;
EltID _currentId;
int _height;

static const int MARGIN;
};

#endif // TABLEHEADERVIEW_H
75 changes: 75 additions & 0 deletions sources/editor/widgets/tableheaderviewv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/***************************************************************************
** **
** Polyphone, a soundfont editor **
** Copyright (C) 2013-2019 Davy Triponney **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Author: Davy Triponney **
** Website/Contact: https://www.polyphone-soundfonts.com **
** Date: 01.01.2013 **
***************************************************************************/

#include "tableheaderviewv.h"
#include <QPainter>

const int TableHeaderViewV::MARGIN = 2;

TableHeaderViewV::TableHeaderViewV(QWidget *parent) : QHeaderView(Qt::Vertical, parent)
{
this->setSectionResizeMode(QHeaderView::Fixed);
this->setSectionsClickable(true);
this->setHighlightSections(true);
}

void TableHeaderViewV::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
// Get the text and icon to display
QString text = this->model()->headerData(logicalIndex, this->orientation(), Qt::DisplayRole).toString();
QPixmap icon = this->model()->headerData(logicalIndex, this->orientation(), Qt::DecorationRole).value<QPixmap>();

// Icon and text rect
QRect iconRect = icon.rect();
QRect textRect = rect;
textRect.setWidth(textRect.width() - 2 * MARGIN);
if (iconRect.width() > 0)
textRect.setWidth(textRect.width() - iconRect.width() - MARGIN);
textRect.translate(MARGIN, 0);

// Elide the text
QFontMetrics fm(this->font());
QString adaptedText = fm.elidedText(text, Qt::ElideRight, textRect.width());

// First draw the cell without text or icon for the background and border
this->model()->setHeaderData(logicalIndex, this->orientation(), "", Qt::DisplayRole);
this->model()->setHeaderData(logicalIndex, this->orientation(), QVariant(), Qt::DecorationRole);
QHeaderView::paintSection(painter, rect, logicalIndex);
this->model()->setHeaderData(logicalIndex, this->orientation(), text, Qt::DisplayRole);
this->model()->setHeaderData(logicalIndex, this->orientation(), icon, Qt::DecorationRole);

// Then draw the text
QVariant foregroundBrush = model()->headerData(logicalIndex, this->orientation(), Qt::ForegroundRole);
if (foregroundBrush.canConvert<QBrush>())
painter->setPen(foregroundBrush.value<QBrush>().color());
painter->setClipRect(rect);
painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, adaptedText);

// Finally draw the icon on the right and vertically centered
int iconHeight = iconRect.height();
int headerHeight = rect.height();
int offsetY = iconHeight < headerHeight ? (headerHeight - iconHeight) / 2 : 0;
painter->setClipRect(rect);
painter->drawPixmap(textRect.right() + MARGIN, rect.top() + offsetY, iconRect.width(), iconRect.height(), icon);
}
44 changes: 44 additions & 0 deletions sources/editor/widgets/tableheaderviewv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/***************************************************************************
** **
** Polyphone, a soundfont editor **
** Copyright (C) 2013-2019 Davy Triponney **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Author: Davy Triponney **
** Website/Contact: https://www.polyphone-soundfonts.com **
** Date: 01.01.2013 **
***************************************************************************/

#ifndef TABLEHEADERVIEWV_H
#define TABLEHEADERVIEWV_H

#include <QHeaderView>

class TableHeaderViewV : public QHeaderView
{
Q_OBJECT

public:
TableHeaderViewV(QWidget *parent = nullptr);

protected:
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;

private:
static const int MARGIN;
};

#endif // TABLEHEADERVIEWV_H
6 changes: 3 additions & 3 deletions sources/editor/widgets/tablewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
#include <QHeaderView>
#include <QScrollBar>
#include "tableheaderview.h"
#include "tableheaderviewv.h"

TableWidget::TableWidget(QWidget *parent) : QTableWidget(parent)
{
_tableDelegate = new TableDelegate(this);
setItemDelegate(_tableDelegate);
_tableHeader = new TableHeaderView(this);
setHorizontalHeader(_tableHeader);
this->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
setHorizontalHeader(new TableHeaderView(this));
setVerticalHeader(new TableHeaderViewV(this));

_timer = new QTimer(this);
connect(_timer, SIGNAL(timeout()), this, SLOT(updateColors()));
Expand Down
2 changes: 0 additions & 2 deletions sources/editor/widgets/tablewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <QTimer>
#include "soundfontmanager.h"
class TableDelegate;
class TableHeaderView;

// QTableWidget an id by column, double click triggers an event and blue effect on the header
class TableWidget : public QTableWidget
Expand Down Expand Up @@ -82,7 +81,6 @@ private slots:
QTimer *_timer;
QList<QColor> _listColors;
TableDelegate * _tableDelegate;
TableHeaderView * _tableHeader;
QPixmap _muteIcon;

void copy();
Expand Down
2 changes: 2 additions & 0 deletions sources/polyphone.pro
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ SOURCES += main.cpp \
editor/overview/pageoverviewprst.cpp \
editor/overview/tablepageoverview.cpp \
editor/widgets/equalizer.cpp \
editor/widgets/tableheaderviewv.cpp \
editor/widgets/tablewidget.cpp \
editor/widgets/tabledelegate.cpp \
editor/widgets/spinboxkey.cpp \
Expand Down Expand Up @@ -523,6 +524,7 @@ HEADERS += \
editor/overview/pageoverviewprst.h \
editor/overview/tablepageoverview.h \
editor/widgets/equalizer.h \
editor/widgets/tableheaderviewv.h \
editor/widgets/tablewidget.h \
editor/widgets/tabledelegate.h \
editor/widgets/spinboxkey.h \
Expand Down

0 comments on commit 9800a43

Please sign in to comment.