3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "iconpalette.h"
14 #include "qt_helpers.h"
15 #include "controllers/ControlMath.h" // for find_xpm
18 #include <QGridLayout>
19 #include <QPushButton>
30 FlowLayout::FlowLayout(QWidget *parent) : QLayout(parent)
37 FlowLayout::~FlowLayout()
40 while ((item = takeAt(0)))
45 void FlowLayout::addItem(QLayoutItem *item)
47 itemList.append(item);
51 int FlowLayout::count() const
53 return itemList.size();
57 QLayoutItem *FlowLayout::itemAt(int index) const
59 return itemList.value(index);
63 QLayoutItem *FlowLayout::takeAt(int index)
65 if (index >= 0 && index < itemList.size())
66 return itemList.takeAt(index);
72 Qt::Orientations FlowLayout::expandingDirections() const
78 bool FlowLayout::hasHeightForWidth() const
84 int FlowLayout::heightForWidth(int width) const
86 int height = doLayout(QRect(0, 0, width, 0), true);
91 void FlowLayout::setGeometry(const QRect &rect)
93 QLayout::setGeometry(rect);
94 doLayout(rect, false);
98 QSize FlowLayout::sizeHint() const
100 // default to (max) 6 columns
101 int const cols = qMin(itemList.size(), 6);
102 int const rows = (itemList.size() - 1 )/6 + 1;
103 return QSize(cols * minimumSize().width() + 1,
104 rows * minimumSize().height() + 1);
108 QSize FlowLayout::minimumSize() const
111 int const n = itemList.size();
112 for (int i = 0; i < n; ++i) {
113 size = size.expandedTo(itemList.at(i)->minimumSize());
119 int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
125 for (int i = 0; i < itemList.size(); ++i) {
126 QLayoutItem * item = itemList.at(i);
127 int nextX = x + item->sizeHint().width() + spacing();
128 if (nextX - spacing() > rect.right() && lineHeight > 0) {
130 y = y + lineHeight + spacing();
131 nextX = x + item->sizeHint().width() + spacing();
136 item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
139 lineHeight = qMax(lineHeight, item->sizeHint().height());
141 return y + lineHeight - rect.y();
145 IconPalette::IconPalette(QWidget * parent, char const ** entries)
148 FlowLayout * layout_ = new FlowLayout(this);
149 layout_->setSpacing(0);
151 int const button_size = 40;
152 for (int i = 0; *entries[i]; ++i) {
153 QPushButton * p = new QPushButton;
154 p->setFixedSize(button_size, button_size);
155 p->setIcon(QPixmap(toqstr(lyx::frontend::find_xpm(entries[i]))));
156 p->setToolTip(toqstr(string("\\") + entries[i]));
157 connect(p, SIGNAL(clicked()), this, SLOT(clicked()));
158 buttons_.push_back(make_pair(p, entries[i]));
159 layout_->addWidget(p);
165 void IconPalette::clicked()
167 vector<Button>::const_iterator it = buttons_.begin();
168 vector<Button>::const_iterator const end = buttons_.end();
169 for (; it != end; ++it) {
170 if (sender() == it->first) {
172 button_clicked(it->second);
179 } // namespace frontend
182 #include "iconpalette_moc.cpp"