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
 
QLayoutItem * itemAt (int i) const override
 
 KoToolBoxLayout (QWidget *parent)
 
QSize minimumSize () const override
 
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

Qt::Orientation m_orientation
 
QList< QWidgetItem * > m_sections
 

Detailed Description

Definition at line 248 of file KoToolBoxLayout_p.h.

Constructor & Destructor Documentation

◆ KoToolBoxLayout()

KoToolBoxLayout::KoToolBoxLayout ( QWidget * parent)
inlineexplicit

Definition at line 251 of file KoToolBoxLayout_p.h.

252 : QLayout(parent)
253 , m_orientation(Qt::Vertical)
254 {
255 setSpacing(6);
256 }
Qt::Orientation m_orientation

◆ ~KoToolBoxLayout()

KoToolBoxLayout::~KoToolBoxLayout ( )
inlineoverride

Definition at line 258 of file KoToolBoxLayout_p.h.

259 {
260 qDeleteAll( m_sections );
261 m_sections.clear();
262 }
QList< QWidgetItem * > m_sections

Member Function Documentation

◆ addItem()

void KoToolBoxLayout::addItem ( QLayoutItem * )
inlineoverride

Definition at line 298 of file KoToolBoxLayout_p.h.

299 {
300 Q_ASSERT(0); // don't let anything else be added. (code depends on this!)
301 }

◆ addSection()

void KoToolBoxLayout::addSection ( Section * section)
inline

Definition at line 284 of file KoToolBoxLayout_p.h.

285 {
286 addChildWidget(section);
287
288 QList<QWidgetItem*>::iterator iterator = m_sections.begin();
289 int defaults = 2; // skip the first two as they are the 'main' and 'dynamic' sections.
290 while (iterator != m_sections.end()) {
291 if (--defaults < 0 && static_cast<Section*> ((*iterator)->widget())->name() > section->name())
292 break;
293 ++iterator;
294 }
295 m_sections.insert(iterator, new QWidgetItem(section));
296 }
QString name() const

References Section::name().

◆ count()

int KoToolBoxLayout::count ( ) const
inlineoverride

Definition at line 310 of file KoToolBoxLayout_p.h.

310{ return m_sections.count(); }

◆ doLayout()

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

Definition at line 367 of file KoToolBoxLayout_p.h.

368 {
369 // nothing to do?
370 if (m_sections.isEmpty()) {
371 return 0;
372 }
373
374 // the names of the variables assume a vertical orientation,
375 // but all calculations are done based on the real orientation
376 const bool isVertical = m_orientation == Qt::Vertical;
377
378 const QSize iconSize = static_cast<Section*> (m_sections.first()->widget())->iconSize();
379
380 const int maxWidth = isVertical ? rect.width() : rect.height();
381 // using min 1 as width to e.g. protect against div by 0 below
382 const int iconWidth = qMax(1, isVertical ? iconSize.width() : iconSize.height());
383 const int iconHeight = qMax(1, isVertical ? iconSize.height() : iconSize.width());
384
385 const int maxColumns = qMax(1, (maxWidth / iconWidth));
386
387 int x = 0;
388 int y = 0;
389 bool firstSection = true;
390 foreach (QWidgetItem *wi, m_sections) {
391 Section *section = static_cast<Section*> (wi->widget());
392 const int buttonCount = section->visibleButtonCount();
393 if (buttonCount == 0) {
394 // move out of view, not perfect TODO: better solution
395 if (notDryRun) {
396 section->setGeometry(1000, 1000, 0, 0);
397 }
398 continue;
399 }
400
401 // rows needed for the buttons (calculation gets the ceiling value of the plain div)
402 const int neededRowCount = ((buttonCount-1) / maxColumns) + 1;
403
404 if (firstSection) {
405 firstSection = false;
406 } else {
407 // start on a new row, set separator
408 x = 0;
409 y += iconHeight + spacing();
410 if (notDryRun){
411 const Section::Separators separator =
413 section->setSeparator( separator );
414 }
415 }
416
417 if (notDryRun) {
418 const int usedColumns = qMin(buttonCount, maxColumns);
419 int narrowSide = usedColumns * iconWidth;
420 int longSide = neededRowCount * iconHeight;
421 if (isVertical) {
422 int realX;
423 if (parentWidget()->isLeftToRight()) {
424 realX = x;
425 } else {
426 realX = rect.width() - x - narrowSide;
427 }
428 section->setGeometry(realX, y,
429 narrowSide, longSide);
430 } else {
431 int realX;
432 if (parentWidget()->isLeftToRight()) {
433 realX = y;
434 } else {
435 realX = rect.width() - y - longSide;
436 }
437 section->setGeometry(realX, x,
438 longSide, narrowSide);
439 }
440 }
441
442 // advance by the icons in the last row
443 const int lastRowColumnCount = buttonCount - ((neededRowCount-1) * maxColumns);
444 x += (lastRowColumnCount * iconWidth) + spacing();
445 // advance by all but the last used row
446 y += (neededRowCount - 1) * iconHeight;
447 }
448
449 // cache total height (or width), adding the iconHeight for the current row
450 return y + iconHeight;
451 }
int iconSize(qreal width, qreal height)
void setSeparator(Separators separators)
int visibleButtonCount() const

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

◆ hasHeightForWidth()

bool KoToolBoxLayout::hasHeightForWidth ( ) const
inlineoverride

Definition at line 318 of file KoToolBoxLayout_p.h.

319 {
320 // return true;
321 return m_orientation == Qt::Vertical;
322 }

◆ heightForWidth()

int KoToolBoxLayout::heightForWidth ( int width) const
inlineoverride

Definition at line 324 of file KoToolBoxLayout_p.h.

325 {
326 if (m_orientation == Qt::Vertical) {
327 const int height = doLayout(QRect(0, 0, width, 0), false);
328 return height;
329 } else {
330 #if 0
331 const int iconHeight = static_cast<Section*> (m_sections[0]->widget())->iconSize().height();
332 for (int i = 1; i <= 10; i++) {
333 const int testWidth = doLayout(QRect(0, 0, 0, iconHeight * i), false);
334 if (testWidth <= width) {
335 return iconHeight * i;
336 }
337 }
338 // Return a huge height
339 return 65535;
340 #endif
341 return -1;
342 }
343 }
int doLayout(const QRect &rect, bool notDryRun) const

References iconSize().

◆ itemAt()

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

Definition at line 303 of file KoToolBoxLayout_p.h.

304 {
305 return m_sections.value(i);
306 }

◆ minimumSize()

QSize KoToolBoxLayout::minimumSize ( ) const
inlineoverride

Definition at line 276 of file KoToolBoxLayout_p.h.

277 {
278 if (m_sections.isEmpty())
279 return QSize();
280 QSize oneIcon = static_cast<Section*> (m_sections[0]->widget())->iconSize();
281 return oneIcon;
282 }

References iconSize().

◆ setGeometry()

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

Definition at line 312 of file KoToolBoxLayout_p.h.

313 {
314 QLayout::setGeometry(rect);
315 doLayout(rect, true);
316 }

◆ setOrientation()

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

Definition at line 360 of file KoToolBoxLayout_p.h.

361 {
362 m_orientation = orientation;
363 invalidate();
364 }

◆ sizeHint()

QSize KoToolBoxLayout::sizeHint ( ) const
inlineoverride

Definition at line 264 of file KoToolBoxLayout_p.h.

265 {
266 // Prefer showing two rows/columns by default
267 QSize twoIcons = static_cast<Section*> (m_sections[0]->widget())->iconSize() * 2;
268 const int length = doLayout(QRect(QPoint(), twoIcons), false);
269 if (m_orientation == Qt::Vertical) {
270 return QSize(twoIcons.width(), length);
271 } else {
272 return QSize(length, twoIcons.height());
273 }
274 }
qreal length(const QPointF &vec)
Definition Ellipse.cc:82

References iconSize(), and length().

◆ takeAt()

QLayoutItem * KoToolBoxLayout::takeAt ( int i)
inlineoverride

Definition at line 308 of file KoToolBoxLayout_p.h.

308{ 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 350 of file KoToolBoxLayout_p.h.

351 {
352 if (m_orientation == Qt::Horizontal) {
353 const int width = doLayout(QRect(0, 0, 0, height), false);
354 return width;
355 } else {
356 return -1;
357 }
358 }

Member Data Documentation

◆ m_orientation

Qt::Orientation KoToolBoxLayout::m_orientation
private

Definition at line 454 of file KoToolBoxLayout_p.h.

◆ m_sections

QList<QWidgetItem*> KoToolBoxLayout::m_sections
private

Definition at line 453 of file KoToolBoxLayout_p.h.


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