Krita Source Code Documentation
Loading...
Searching...
No Matches
KoToolBoxLayout Class Reference

#include <KoToolBoxLayout_p.h>

+ Inheritance diagram for KoToolBoxLayout:

Public Member Functions

void addItem (QLayoutItem *) override
 
void addSection (Section *section)
 
int count () const override
 
bool hasHeightForWidth () const override
 
int heightForWidth (int width) const override
 
bool isCompact ()
 
QLayoutItem * itemAt (int i) const override
 
 KoToolBoxLayout (QWidget *parent)
 
QSize minimumSize () const override
 
void setCompact (bool state)
 
void setGeometry (const QRect &rect) override
 
void setOrientation (Qt::Orientation orientation)
 
QSize sizeHint () const override
 
QLayoutItem * takeAt (int i) override
 
int widthForHeight (int height) const
 
 ~KoToolBoxLayout () override
 

Private Member Functions

int doLayout (const QRect &rect, bool notDryRun) const
 

Private Attributes

bool m_compact
 
Qt::Orientation m_orientation
 
QList< QWidgetItem * > m_sections
 

Detailed Description

Definition at line 257 of file KoToolBoxLayout_p.h.

Constructor & Destructor Documentation

◆ KoToolBoxLayout()

KoToolBoxLayout::KoToolBoxLayout ( QWidget * parent)
inlineexplicit

Definition at line 260 of file KoToolBoxLayout_p.h.

261 : QLayout(parent)
262 , m_orientation(Qt::Vertical)
263 , m_compact(false)
264 {
265 setSpacing(6);
266 }
Qt::Orientation m_orientation

◆ ~KoToolBoxLayout()

KoToolBoxLayout::~KoToolBoxLayout ( )
inlineoverride

Definition at line 268 of file KoToolBoxLayout_p.h.

269 {
270 qDeleteAll( m_sections );
271 m_sections.clear();
272 }
QList< QWidgetItem * > m_sections

Member Function Documentation

◆ addItem()

void KoToolBoxLayout::addItem ( QLayoutItem * )
inlineoverride

Definition at line 308 of file KoToolBoxLayout_p.h.

309 {
310 Q_ASSERT(0); // don't let anything else be added. (code depends on this!)
311 }

◆ addSection()

void KoToolBoxLayout::addSection ( Section * section)
inline

Definition at line 294 of file KoToolBoxLayout_p.h.

295 {
296 addChildWidget(section);
297
298 QList<QWidgetItem*>::iterator iterator = m_sections.begin();
299 int defaults = 2; // skip the first two as they are the 'main' and 'dynamic' sections.
300 while (iterator != m_sections.end()) {
301 if (--defaults < 0 && static_cast<Section*> ((*iterator)->widget())->name() > section->name())
302 break;
303 ++iterator;
304 }
305 m_sections.insert(iterator, new QWidgetItem(section));
306 }
QString name() const

References Section::name().

◆ count()

int KoToolBoxLayout::count ( ) const
inlineoverride

Definition at line 320 of file KoToolBoxLayout_p.h.

320{ return m_sections.count(); }

◆ doLayout()

int KoToolBoxLayout::doLayout ( const QRect & rect,
bool notDryRun ) const
inlineprivate

Definition at line 390 of file KoToolBoxLayout_p.h.

391 {
392 // nothing to do?
393 if (m_sections.isEmpty()) {
394 return 0;
395 }
396
397 // the names of the variables assume a vertical orientation,
398 // but all calculations are done based on the real orientation
399 const bool isVertical = m_orientation == Qt::Vertical;
400 const bool isLeftToRight = parentWidget()->isLeftToRight();
401
402 const QSize iconSize = static_cast<Section*> (m_sections.first()->widget())->iconSize();
403
404 const int maxWidth = isVertical ? rect.width() : rect.height();
405 // using min 1 as width to e.g. protect against div by 0 below
406 const int iconWidth = qMax(1, isVertical ? iconSize.width() : iconSize.height());
407 const int iconHeight = qMax(1, isVertical ? iconSize.height() : iconSize.width());
408
409 const Section::Separators separator = m_compact ?
411 : isVertical ?
413 :
415
416 const int maxColumns = qMax(1, (maxWidth / iconWidth));
417
418 int y = 0;
419 int offset = 0;
420 bool firstSection = true;
421 foreach (QWidgetItem *wi, m_sections) {
422 Section *section = static_cast<Section*> (wi->widget());
423 const int buttonCount = section->visibleButtonCount();
424 if (buttonCount == 0) {
425 // move out of view, not perfect TODO: better solution
426 if (notDryRun) {
427 section->setGeometry(1000, 1000, 0, 0);
428 }
429 continue;
430 }
431
432 // in compact mode, the previous section's offset is this section's onset
433 const int onset = m_compact ? offset : 0;
434 const int usedColumns = onset + buttonCount;
435 offset = usedColumns % maxColumns;
436
437 // rows needed for the buttons (calculation gets the ceiling value of the plain div)
438 const int neededRowCount = ((usedColumns - 1) / maxColumns) + 1;
439
440 if (firstSection) {
441 firstSection = false;
442 } else {
443 if (m_compact) {
444 // start on a new row if the current row is full
445 y += (onset == 0) ? iconHeight : 0;
446 } else {
447 // start on a new row, set separator
448 y += iconHeight + spacing();
449 }
450 if (notDryRun) {
451 section->setOnset(onset);
452 section->setSeparator(separator);
453 }
454 }
455
456 if (notDryRun) {
457 const int onW = onset * iconWidth;
458 const int offW = offset * iconWidth;
459 const int width = maxColumns * iconWidth;
460 const int height = neededRowCount * iconHeight;
461
462 QRect geometry;
463 QRegion mask;
464 if (isVertical) {
465 mask = QRegion(0, 0, width, height);
466 if (isLeftToRight) {
467 geometry = QRect(0, y, width, height);
468
469 // mask onset and offset regions
470 // so that they don't block mouse events to other sections
471 mask -= QRegion(0, 0, onW, iconHeight);
472 if (offset != 0) {
473 mask -= QRegion(offW, height - iconHeight, width - offW, iconHeight);
474 }
475 } else {
476 geometry = QRect(rect.width() - width, y, width, height);
477 mask -= QRegion(width - onW, 0, onW, iconHeight);
478 if (offset != 0) {
479 mask -= QRegion(0, height - iconHeight, width - offW, iconHeight);
480 }
481 }
482 } else {
483 mask = QRegion(0, 0, height, width);
484 if (isLeftToRight) {
485 geometry = QRect(y, 0, height, width);
486 mask -= QRegion(0, 0, iconHeight, onW);
487 if (offset != 0) {
488 mask -= QRegion(height - iconHeight, offW, iconHeight, width - offW);
489 }
490 } else {
491 geometry = QRect(rect.width() - y - height, 0, height, width);
492 mask -= QRegion(height - iconHeight, 0, iconHeight, onW);
493 if (offset != 0) {
494 mask -= QRegion(0, offW, iconHeight, width - offW);
495 }
496 }
497 }
498 section->setGeometry(geometry);
499 section->setMask(mask);
500 }
501
502 // advance by all but the last used row
503 y += (neededRowCount - 1) * iconHeight;
504 }
505
506 // cache total height (or width), adding the iconHeight for the current row
507 return y + iconHeight;
508 }
int iconSize(qreal width, qreal height)
void setOnset(int onset)
void setSeparator(Separators separators)
int visibleButtonCount() const

References iconSize(), Section::SeparatorLeft, Section::SeparatorNone, Section::SeparatorTop, Section::setOnset(), Section::setSeparator(), and Section::visibleButtonCount().

◆ hasHeightForWidth()

bool KoToolBoxLayout::hasHeightForWidth ( ) const
inlineoverride

Definition at line 328 of file KoToolBoxLayout_p.h.

329 {
330 // return true;
331 return m_orientation == Qt::Vertical;
332 }

◆ heightForWidth()

int KoToolBoxLayout::heightForWidth ( int width) const
inlineoverride

Definition at line 334 of file KoToolBoxLayout_p.h.

335 {
336 if (m_orientation == Qt::Vertical) {
337 const int height = doLayout(QRect(0, 0, width, 0), false);
338 return height;
339 } else {
340 #if 0
341 const int iconHeight = static_cast<Section*> (m_sections[0]->widget())->iconSize().height();
342 for (int i = 1; i <= 10; i++) {
343 const int testWidth = doLayout(QRect(0, 0, 0, iconHeight * i), false);
344 if (testWidth <= width) {
345 return iconHeight * i;
346 }
347 }
348 // Return a huge height
349 return 65535;
350 #endif
351 return -1;
352 }
353 }
int doLayout(const QRect &rect, bool notDryRun) const

References iconSize().

◆ isCompact()

bool KoToolBoxLayout::isCompact ( )
inline

Definition at line 384 of file KoToolBoxLayout_p.h.

385 {
386 return m_compact;
387 }

◆ itemAt()

QLayoutItem * KoToolBoxLayout::itemAt ( int i) const
inlineoverride

Definition at line 313 of file KoToolBoxLayout_p.h.

314 {
315 return m_sections.value(i);
316 }

◆ minimumSize()

QSize KoToolBoxLayout::minimumSize ( ) const
inlineoverride

Definition at line 286 of file KoToolBoxLayout_p.h.

287 {
288 if (m_sections.isEmpty())
289 return QSize();
290 QSize oneIcon = static_cast<Section*> (m_sections[0]->widget())->iconSize();
291 return oneIcon;
292 }

References iconSize().

◆ setCompact()

void KoToolBoxLayout::setCompact ( bool state)
inline

Definition at line 376 of file KoToolBoxLayout_p.h.

377 {
378 m_compact = state;
379 foreach (QWidgetItem *wi, m_sections) {
380 wi->widget()->layout()->invalidate();
381 }
382 }

◆ setGeometry()

void KoToolBoxLayout::setGeometry ( const QRect & rect)
inlineoverride

Definition at line 322 of file KoToolBoxLayout_p.h.

323 {
324 QLayout::setGeometry(rect);
325 doLayout(rect, true);
326 }

◆ setOrientation()

void KoToolBoxLayout::setOrientation ( Qt::Orientation orientation)
inline

Definition at line 370 of file KoToolBoxLayout_p.h.

371 {
372 m_orientation = orientation;
373 invalidate();
374 }

◆ sizeHint()

QSize KoToolBoxLayout::sizeHint ( ) const
inlineoverride

Definition at line 274 of file KoToolBoxLayout_p.h.

275 {
276 // Prefer showing two rows/columns by default
277 QSize twoIcons = static_cast<Section*> (m_sections[0]->widget())->iconSize() * 2;
278 const int length = doLayout(QRect(QPoint(), twoIcons), false);
279 if (m_orientation == Qt::Vertical) {
280 return QSize(twoIcons.width(), length);
281 } else {
282 return QSize(length, twoIcons.height());
283 }
284 }
qreal length(const QPointF &vec)
Definition Ellipse.cc:82

References iconSize(), and length().

◆ takeAt()

QLayoutItem * KoToolBoxLayout::takeAt ( int i)
inlineoverride

Definition at line 318 of file KoToolBoxLayout_p.h.

318{ return m_sections.takeAt(i); }

◆ widthForHeight()

int KoToolBoxLayout::widthForHeight ( int height) const
inline

For calculating the width from height by KoToolBoxScrollArea. QWidget doesn't actually support trading width for height, so it needs to be handled specifically.

Definition at line 360 of file KoToolBoxLayout_p.h.

361 {
362 if (m_orientation == Qt::Horizontal) {
363 const int width = doLayout(QRect(0, 0, 0, height), false);
364 return width;
365 } else {
366 return -1;
367 }
368 }

Member Data Documentation

◆ m_compact

bool KoToolBoxLayout::m_compact
private

Definition at line 512 of file KoToolBoxLayout_p.h.

◆ m_orientation

Qt::Orientation KoToolBoxLayout::m_orientation
private

Definition at line 511 of file KoToolBoxLayout_p.h.

◆ m_sections

QList<QWidgetItem*> KoToolBoxLayout::m_sections
private

Definition at line 510 of file KoToolBoxLayout_p.h.


The documentation for this class was generated from the following file: