void PaletteToolbar::arrangeButtons()
{
if (buttonList.size()!=0)
{
arranging = true;
foreach(PaletteToolbarButton* button, buttonList)
{
button->hide();
}
expandButton->hide();
int cellWidth = buttonList.first()->minimumWidth()+2;
int cellHeight = buttonList.first()->minimumHeight()+2;
int maxWidth = cellWidth, maxHeight = cellHeight;
bool vert = orientation()==Qt::Vertical;
switch(static_cast<QMainWindow*>(parentWidget())->toolBarArea(this))
{
case Qt::LeftToolBarArea:
expandButton->setArrowType(expanded? Qt::LeftArrow : Qt::RightArrow);
break;
case Qt::RightToolBarArea:
expandButton->setArrowType(expanded? Qt::RightArrow : Qt::LeftArrow);
break;
case Qt::BottomToolBarArea:
expandButton->setArrowType(expanded? Qt::DownArrow : Qt::UpArrow);
break;
case Qt::TopToolBarArea:
expandButton->setArrowType(expanded? Qt::UpArrow : Qt::DownArrow);
break;
}
if (!expanded)
{
int x, y;
if (vert)
{
//setMinimumWidth(cellWidth);
foreach
(QToolBar* toolBar, getToolBarsAtThisSide
()) {
if (toolBar->width() > maxWidth)
maxWidth = toolBar->width();
}
//setMaximumWidth(maxWidth);
resize(maxWidth,height());
y = 10;
x = (width()-cellWidth)/2;
}
else
{
//setMinimumHeight(cellHeight);
foreach
(QToolBar* toolBar, getToolBarsAtThisSide
()) {
if (toolBar->height() > maxHeight)
maxHeight = toolBar->height();
}
//setMaximumHeight(maxHeight);
resize(width(),maxHeight);
x = 10;
y = (height()-cellHeight)/2;
}
foreach(PaletteToolbarButton* button, buttonList)
{
button->move(x,y);
button->show();
if (vert)
{
y += cellHeight;
if (y + cellHeight*2 > height()) break;
}
else
{
x += cellWidth;
if (x + cellWidth*2 > width()) break;
}
}
expandButton->move(x,y);
expandButton->show();
}
else //PaletteBar is expanded
{
int x, y, buttonStrings, maxButtonsInString;
if (vert)
{
maxButtonsInString = height() / cellHeight;
buttonStrings = (buttonList.size() + 1) / maxButtonsInString;
if (buttonList.size() % maxButtonsInString) ++buttonStrings;
//setMinimumWidth(cellWidth * buttonStrings);
//QPoint newpos = pos();
//newpos.setX(newpos.x() - (cellWidth * buttonStrings - 1));
//move(newpos);
resize(cellWidth * buttonStrings,height());
y = 10;
x = 1;
}
else
{
maxButtonsInString = width() / cellWidth;
buttonStrings = (buttonList.size() + 1) / maxButtonsInString;
if (buttonList.size() % maxButtonsInString) ++buttonStrings;
//setMinimumHeight(cellHeight * buttonStrings);
//QPoint newpos = pos();
//newpos.setY(newpos.y() + (cellHeight * buttonStrings - 1));
//move(newpos);
resize(width(),cellHeight * buttonStrings);
y = 1;
x = 10;
}
foreach(PaletteToolbarButton* button, buttonList)
{
button->move(x,y);
button->show();
if (vert)
{
y += cellHeight;
if (y + cellHeight*2 > height())
{
y = 10;
x += cellWidth;
}
}
else
{
x += cellWidth;
if (x + cellWidth*2 > width())
{
x = 10;
y += cellHeight;
}
}
}
expandButton->move(x,y);
expandButton->show();
if (vert)
{
maxButtonsInString = height() / cellHeight;
buttonStrings = (buttonList.size() + 1) / maxButtonsInString;
if (buttonList.size() % maxButtonsInString) ++buttonStrings;
//setMinimumWidth(cellWidth * buttonStrings);
//QPoint newpos = pos();
//newpos.setX(newpos.x() - (cellWidth * buttonStrings - 1));
//move(newpos);
resize(cellWidth * buttonStrings,height());
y = 10;
x = 1;
}
else
{
maxButtonsInString = width() / cellWidth;
buttonStrings = (buttonList.size() + 1) / maxButtonsInString;
if (buttonList.size() % maxButtonsInString) ++buttonStrings;
//setMinimumHeight(cellHeight * buttonStrings);
//QPoint newpos = pos();
//newpos.setY(newpos.y() + (cellHeight * buttonStrings - 1));
//move(newpos);
resize(width(),cellHeight * buttonStrings);
y = 1;
x = 10;
}
}
arranging = false;
}//if (buttonList.size()!=0)
}
QList<QToolBar*> PaletteToolbar::getToolBarsAtThisSide()
{
QList<QToolBar*> ret;
QMainWindow* mainWnd
= static_cast<QMainWindow
*>
(parentWidget
());
Qt::ToolBarArea area = mainWnd->toolBarArea(this);
foreach
(QObject* object, mainWnd
->children
()) {
if (object!=this)
if (QToolBar* toolBar
= dynamic_cast<QToolBar
*>
(object
)) {
if (area == mainWnd->toolBarArea(toolBar))
ret.append(toolBar);
}
}
return ret;
}
Bookmarks