Allow columns with percentage width

This commit is contained in:
Klaus Basan
2018-12-25 10:32:39 +01:00
committed by Mat Sutcliffe
parent 73d8cf392b
commit 7baf5f238e
5 changed files with 97 additions and 2 deletions

View File

@@ -185,6 +185,58 @@ namespace BlackGui
return column >= 0 && column < m_columns.size();
}
bool CColumns::hasAnyWidthPercentage() const
{
for (const CColumn &c : m_columns) { if (c.hasWidthPercentage()) { return true; }}
return false;
}
void CColumns::setWidthPercentages(const QList<int> percentages)
{
if (percentages.isEmpty())
{
for (CColumn &column : m_columns) { column.setWidthPercentage(-1); }
return;
}
int c = 0;
for (CColumn &column : m_columns)
{
column.setWidthPercentage(percentages.at(c++));
}
}
QList<int> CColumns::calculateWidths(int totalWidth) const
{
if (m_columns.isEmpty() || !this->hasAnyWidthPercentage()) { return {}; }
int totalPercentage = 0;
const int averagePercentage = 100 / m_columns.size();
for (const CColumn &c : m_columns)
{
totalPercentage += c.hasWidthPercentage() ? c.getWidthPercentage() : averagePercentage;
}
if (totalPercentage < 1) { return {}; }
// ideally totalPercentage would be 100%, but there is no guarantee
const double part = static_cast<double>(totalWidth) / totalPercentage;
QList<int> widths;
int usedWidth = 0;
for (const CColumn &c : m_columns)
{
const int percentage = c.hasWidthPercentage() ? c.getWidthPercentage() : averagePercentage;
const int restWidth = totalWidth - usedWidth;
const int width = qMin(restWidth, qRound(part * percentage));
widths.push_back(width);
usedWidth += width;
}
return widths;
}
const CDefaultFormatter *CColumns::getFormatter(const QModelIndex &index) const
{
if (!isValidColumn(index)) { return nullptr; }

View File

@@ -92,6 +92,15 @@ namespace BlackGui
//! Translation context
void setTranslationContext(const QString &translationContext) { m_translationContext = translationContext; }
//! Width in percentage
int getWidthPercentage() const { return m_widthPercentage; }
//! Having a width percentage
bool hasWidthPercentage() const { return m_widthPercentage > 0; }
//! Width percentage
void setWidthPercentage(int width) { m_widthPercentage = width; }
//! Get a standard value object formatted column
static CColumn standardValueObject(const QString &headerName, const BlackMisc::CPropertyIndex &propertyIndex, int alignment = CDefaultFormatter::alignDefault());
@@ -114,6 +123,7 @@ namespace BlackGui
QString m_translationContext;
QString m_columnName;
QString m_columnToolTip;
int m_widthPercentage = -1;
QSharedPointer<CDefaultFormatter> m_formatter; //!< Used formatter
BlackMisc::CPropertyIndex m_propertyIndex; //!< Property index for column
BlackMisc::CPropertyIndex m_sortPropertyIndex; //!< Property index used when sorted (optional alternative)
@@ -202,6 +212,15 @@ namespace BlackGui
//! Columns
const QList<CColumn> &columns() const { return m_columns; }
//! Any column with width percentage?
bool hasAnyWidthPercentage() const;
//! Set the width percentages
void setWidthPercentages(const QList<int> percentages);
//! Calculate the absolute width
QList<int> calculateWidths(int totalWidth) const;
private:
QList<CColumn> m_columns;
QString m_translationContext; //!< for future usage

View File

@@ -545,6 +545,19 @@ namespace BlackGui
tw->setTabText(index, o);
}
template<class T>
void CViewBase<T>::setPercentageColumnWidths()
{
const int width = this->width() - 25; // avoid scrollbars etc, reserve a little space
QList<int> widths = this->getColumns().calculateWidths(width);
if (widths.isEmpty()) { return; }
for (int c = 0; c < this->getColumns().size(); c++)
{
const int w = widths.at(c);
this->setColumnWidth(c, w);
}
}
template <class T>
void CViewBase<T>::setSortIndicator()
{

View File

@@ -301,6 +301,9 @@ namespace BlackGui
//! Workaround as of https://stackoverflow.com/q/3433664/356726
void setForceColumnsToMaxSize(bool force) { m_forceColumnsToMaxSize = force; }
//! Resize mode
void setHorizontalHeaderSectionResizeMode(QHeaderView::ResizeMode mode);
//! Index of the directory we "remember"
void setSettingsDirectoryIndex(BlackMisc::CDirectories::ColumnIndex directoryIndex) { m_dirSettingsIndex = directoryIndex; }
@@ -744,6 +747,10 @@ namespace BlackGui
//! Set a tab widget text based on row count, filter etc.
void setTabWidgetViewText(QTabWidget *tw, int index);
//! Set the widths based on the column percentages
//! \sa CColumn::get
void setPercentageColumnWidths();
protected:
ModelClass *m_model = nullptr; //!< corresponding model

View File

@@ -39,8 +39,7 @@ namespace BlackGui
{
namespace Views
{
CViewBaseNonTemplate::CViewBaseNonTemplate(QWidget *parent) :
QTableView(parent)
CViewBaseNonTemplate::CViewBaseNonTemplate(QWidget *parent) : QTableView(parent)
{
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &QWidget::customContextMenuRequested, this, &CViewBaseNonTemplate::customMenuRequested);
@@ -186,6 +185,11 @@ namespace BlackGui
return this->ps_saveJson(selectedOnly, directory);
}
void CViewBaseNonTemplate::setHorizontalHeaderSectionResizeMode(QHeaderView::ResizeMode mode)
{
this->horizontalHeader()->setSectionResizeMode(mode);
}
IMenuDelegate *CViewBaseNonTemplate::setCustomMenu(IMenuDelegate *menu, bool nestPreviousMenu)
{
if (menu && nestPreviousMenu)