void RDHeaderView::resizeSections(QHeaderView::ResizeMode mode) { if(!m_sectionStretchHints.isEmpty()) { resizeSectionsWithHints(); return; } if(!m_customSizing) return resizeSections(mode); if(mode != ResizeToContents) return; QAbstractItemModel *m = this->model(); int rowCount = m->rowCount(); for(int col = 0; col < m_sections.count(); col++) { QSize sz; for(int row = 0; row < rowCount; row++) { QVariant v = m->data(m->index(row, col), Qt::SizeHintRole); if(v.isValid() && v.canConvert<QSize>()) sz = sz.expandedTo(v.value<QSize>()); } int oldSize = m_sections[col].size; m_sections[col].size = sz.width(); emit sectionResized(col, oldSize, sz.width()); } }
void RDHeaderView::resizeSectionsWithHints() { if(m_sectionMinSizes.count() == 0) return; QVector<int> sizes = m_sectionMinSizes; int available = 0; if(orientation() == Qt::Horizontal) available = rect().width(); else available = rect().height(); // see if we even have any extra space to allocate if(available > m_sectionMinSizesTotal) { // this is how much space we can allocate to stretch sections available -= m_sectionMinSizesTotal; // distribute the available space between the sections. Dividing by the total stretch tells us // how many 'whole' multiples we can allocate: int wholeMultiples = available / m_sectionStretchHintTotal; if(wholeMultiples > 0) { for(int i = 0; i < sizes.count() && i < m_sectionStretchHints.count(); i++) { int hint = m_sectionStretchHints[i]; if(hint > 0) sizes[i] += wholeMultiples * hint; } } available -= wholeMultiples * m_sectionStretchHintTotal; // we now have a small amount (less than m_sectionStretchHintTotal) of extra space to allocate. // we still want to assign this leftover proportional to the hints, otherwise we'd end up with a // stair-stepping effect. // To do this we calculate hint/total for each section then loop around adding on fractional // components to the sizes until one is above 1, then it gets a pixel, and we keep going until // all the remainder is allocated QVector<float> fractions, increment; fractions.resize(sizes.count()); increment.resize(sizes.count()); // set up increments for(int i = 0; i < sizes.count(); i++) { // don't assign any space to sections with negative hints, or sections without hints if(i >= m_sectionStretchHints.count() || m_sectionStretchHints[i] <= 0) { increment[i] = 0.0f; continue; } increment[i] = float(m_sectionStretchHints[i]) / float(m_sectionStretchHintTotal); } while(available > 0) { // loop along each section incrementing it. for(int i = 0; i < fractions.count(); i++) { fractions[i] += increment[i]; // if we have a whole pixel now, assign it if(fractions[i] > 1.0f) { fractions[i] -= 1.0f; sizes[i]++; available--; // if we've assigned all pixels, stop if(available == 0) break; } } } for(int pix = 0; pix < available; pix++) { int minSection = 0; for(int i = 1; i < sizes.count(); i++) { // don't assign any space to sections with negative hints if(i < m_sectionStretchHints.count() && m_sectionStretchHints[i] <= 0) continue; if(sizes[i] < sizes[minSection]) minSection = i; } sizes[minSection]++; } } resizeSections(sizes.toList()); }
ColorListWidget::ColorListWidget(Map* map, MainWindow* window, QWidget* parent) : QWidget(parent) , map(map) , window(window) { react_to_changes = true; setWhatsThis(Util::makeWhatThis("color_dock_widget.html")); // Color table color_table = new QTableWidget(map->getNumColors(), 7); color_table->setEditTriggers(QAbstractItemView::SelectedClicked | QAbstractItemView::AnyKeyPressed); color_table->setSelectionMode(QAbstractItemView::SingleSelection); color_table->setSelectionBehavior(QAbstractItemView::SelectRows); color_table->verticalHeader()->setVisible(false); color_table->setHorizontalHeaderLabels(QStringList() << QString{} << tr("Name") << tr("Spot color") << tr("CMYK") << tr("RGB") << tr("K.o.") << tr("Opacity") ); color_table->setItemDelegateForColumn(0, new ColorItemDelegate(this)); color_table->setItemDelegateForColumn(6, new PercentageDelegate(this)); color_table->setColumnHidden(6, true); auto new_button_menu = new QMenu(this); (void) new_button_menu->addAction(tr("New"), this, SLOT(newColor())); duplicate_action = new_button_menu->addAction(tr("Duplicate"), this, SLOT(duplicateColor())); duplicate_action->setIcon(QIcon(QString::fromLatin1(":/images/tool-duplicate.png"))); // Buttons auto new_button = newToolButton(QIcon(QString::fromLatin1(":/images/plus.png")), tr("New")); new_button->setPopupMode(QToolButton::DelayedPopup); // or MenuButtonPopup new_button->setMenu(new_button_menu); delete_button = newToolButton(QIcon(QString::fromLatin1(":/images/minus.png")), tr("Delete")); auto add_remove_layout = new SegmentedButtonLayout(); add_remove_layout->addWidget(new_button); add_remove_layout->addWidget(delete_button); move_up_button = newToolButton(QIcon(QString::fromLatin1(":/images/arrow-up.png")), tr("Move Up")); move_up_button->setAutoRepeat(true); move_down_button = newToolButton(QIcon(QString::fromLatin1(":/images/arrow-down.png")), tr("Move Down")); move_down_button->setAutoRepeat(true); auto up_down_layout = new SegmentedButtonLayout(); up_down_layout->addWidget(move_up_button); up_down_layout->addWidget(move_down_button); // TODO: In Mapper >= 0.6, switch to ColorWidget (or generic) translation context. edit_button = newToolButton(QIcon(QString::fromLatin1(":/images/settings.png")), QApplication::translate("OpenOrienteering::MapEditorController", "&Edit").remove(QLatin1Char('&'))); edit_button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); auto help_button = newToolButton(QIcon(QString::fromLatin1(":/images/help.png")), tr("Help")); help_button->setAutoRaise(true); // The buttons row layout auto buttons_group_layout = new QHBoxLayout(); buttons_group_layout->addLayout(add_remove_layout); buttons_group_layout->addLayout(up_down_layout); buttons_group_layout->addWidget(edit_button); buttons_group_layout->addWidget(new QLabel(QString::fromLatin1(" ")), 1); buttons_group_layout->addWidget(help_button); // The layout of all components below the table auto bottom_layout = new QVBoxLayout(); QStyleOption style_option(QStyleOption::Version, QStyleOption::SO_DockWidget); bottom_layout->setContentsMargins( style()->pixelMetric(QStyle::PM_LayoutLeftMargin, &style_option) / 2, 0, // Covered by the main layout's spacing. style()->pixelMetric(QStyle::PM_LayoutRightMargin, &style_option) / 2, style()->pixelMetric(QStyle::PM_LayoutBottomMargin, &style_option) / 2 ); bottom_layout->addLayout(buttons_group_layout); bottom_layout->addWidget(new QLabel(tr("Double-click a color value to open a dialog."))); // The main layout auto layout = new QVBoxLayout(); layout->setContentsMargins(QMargins()); layout->addWidget(color_table, 1); layout->addLayout(bottom_layout); setLayout(layout); for (int i = 0; i < map->getNumColors(); ++i) addRow(i); auto header_view = color_table->horizontalHeader(); header_view->setSectionResizeMode(QHeaderView::Interactive); header_view->resizeSections(QHeaderView::ResizeToContents); header_view->setSectionResizeMode(0, QHeaderView::Fixed); // Color header_view->resizeSection(0, 32); header_view->setSectionResizeMode(1, QHeaderView::Stretch); // Name header_view->setSectionResizeMode(1, QHeaderView::Interactive); // Spot colors header_view->setSectionResizeMode(5, QHeaderView::Fixed); // Knockout header_view->resizeSection(5, 32); header_view->setSectionsClickable(false); currentCellChange(color_table->currentRow(), 0, 0, 0); // enable / disable move color buttons // Connections connect(color_table, &QTableWidget::cellChanged, this, &ColorListWidget::cellChange); connect(color_table, &QTableWidget::currentCellChanged, this, &ColorListWidget::currentCellChange); connect(color_table, &QTableWidget::cellDoubleClicked, this, &ColorListWidget::editCurrentColor); connect(new_button, &QAbstractButton::clicked, this, &ColorListWidget::newColor); connect(delete_button, &QAbstractButton::clicked, this, &ColorListWidget::deleteColor); connect(move_up_button, &QAbstractButton::clicked, this, &ColorListWidget::moveColorUp); connect(move_down_button, &QAbstractButton::clicked, this, &ColorListWidget::moveColorDown); connect(edit_button, &QAbstractButton::clicked, this, &ColorListWidget::editCurrentColor); connect(help_button, &QAbstractButton::clicked, this, &ColorListWidget::showHelp); connect(map, &Map::colorAdded, this, &ColorListWidget::colorAdded); connect(map, &Map::colorChanged, this, &ColorListWidget::colorChanged); connect(map, &Map::colorDeleted, this, &ColorListWidget::colorDeleted); }