add item size auto adjust

Change-Id: I1b837ddafc3a4c4b14deb161e82d4cbca0cc9897
This commit is contained in:
石博文 2016-06-22 15:07:53 +08:00 committed by Hualet Wang
parent 7d94c04f58
commit fc746d4505
8 changed files with 100 additions and 20 deletions

View File

@ -44,10 +44,7 @@ void DockItemController::itemMove(DockItem * const moveItem, DockItem * const re
m_itemList.indexOf(replaceItem);
m_itemList.removeAt(moveIndex);
// emit itemRemoved(moveItem);
m_itemList.insert(replaceIndex, moveItem);
// emit itemInserted(replaceIndex, moveItem);
emit itemMoved(moveItem, replaceIndex);
// for app move, index 0 is launcher item, need to pass it.

View File

@ -60,13 +60,6 @@ void AppItem::paintEvent(QPaintEvent *e)
if (m_draging || !m_itemEntry->isValid())
return;
// const int iconSize = std::min(itemRect.width(), itemRect.height());
// QRect iconRect;
// iconRect.setWidth(iconSize);
// iconRect.setHeight(iconSize);
// iconRect.moveTopLeft(itemRect.center() - iconRect.center());
QPainter painter(this);
if (!painter.isActive())
return;
@ -84,10 +77,6 @@ void AppItem::paintEvent(QPaintEvent *e)
// draw icon
painter.drawPixmap(itemRect.center() - m_icon.rect().center(), m_icon);
// draw text
// painter.setPen(Qt::red);
// painter.drawText(rect(), m_itemEntry->title());
}
void AppItem::mouseReleaseEvent(QMouseEvent *e)

View File

@ -14,6 +14,9 @@ void LauncherItem::paintEvent(QPaintEvent *e)
{
DockItem::paintEvent(e);
if (!isVisible())
return;
QPainter painter(this);
painter.drawPixmap(rect().center() - m_icon.rect().center(), m_icon);
}

View File

@ -3,6 +3,7 @@
PlaceholderItem::PlaceholderItem(QWidget *parent)
: DockItem(Placeholder, parent)
{
setBaseSize(0, 0);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
@ -10,3 +11,8 @@ void PlaceholderItem::mousePressEvent(QMouseEvent *e)
{
QWidget::mousePressEvent(e);
}
void PlaceholderItem::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
}

View File

@ -12,6 +12,7 @@ public:
private:
void mousePressEvent(QMouseEvent *e);
void paintEvent(QPaintEvent *e);
};
#endif // PLACEHOLDERITEM_H

View File

@ -11,6 +11,7 @@ MainPanel::MainPanel(QWidget *parent)
m_position(Dock::Top),
m_itemLayout(new QBoxLayout(QBoxLayout::LeftToRight, this)),
m_itemAdjustTimer(new QTimer(this)),
m_itemController(DockItemController::instance(this))
{
m_itemLayout->setSpacing(0);
@ -27,6 +28,10 @@ MainPanel::MainPanel(QWidget *parent)
connect(m_itemController, &DockItemController::itemInserted, this, &MainPanel::itemInserted);
connect(m_itemController, &DockItemController::itemRemoved, this, &MainPanel::itemRemoved);
connect(m_itemController, &DockItemController::itemMoved, this, &MainPanel::itemMoved);
connect(m_itemAdjustTimer, &QTimer::timeout, this, &MainPanel::adjustItemSize);
m_itemAdjustTimer->setSingleShot(true);
m_itemAdjustTimer->setInterval(200);
const QList<DockItem *> itemList = m_itemController->itemList();
for (auto item : itemList)
@ -50,14 +55,14 @@ void MainPanel::updateDockPosition(const Position dockPosition)
case Position::Right: m_itemLayout->setDirection(QBoxLayout::TopToBottom); break;
}
adjustItemSize();
m_itemAdjustTimer->start();
}
void MainPanel::resizeEvent(QResizeEvent *e)
{
QWidget::resizeEvent(e);
adjustItemSize();
m_itemAdjustTimer->start();
}
void MainPanel::dragEnterEvent(QDragEnterEvent *e)
@ -115,6 +120,8 @@ DockItem *MainPanel::itemAt(const QPoint &point)
void MainPanel::adjustItemSize()
{
Q_ASSERT(sender() == m_itemAdjustTimer);
QSize itemSize;
switch (m_position)
{
@ -134,16 +141,93 @@ void MainPanel::adjustItemSize()
Q_ASSERT(false);
}
if (itemSize.height() < 0 || itemSize.width() < 0)
return;
int totalAppItemCount = 0;
int totalWidth = 0;
int totalHeight = 0;
const QList<DockItem *> itemList = m_itemController->itemList();
for (auto item : itemList)
{
switch (item->itemType())
{
case DockItem::App:
case DockItem::Launcher:
case DockItem::App: item->setFixedSize(itemSize); break;
item->setFixedSize(itemSize);
++totalAppItemCount;
totalWidth += itemSize.width();
totalHeight += itemSize.height();
break;
case DockItem::Plugins:
totalWidth += item->sizeHint().width();
totalHeight += item->sizeHint().height();
break;
default:;
}
}
// test if panel can display all items completely
bool containsCompletely = false;
switch (m_position)
{
case Dock::Top:
case Dock::Bottom:
containsCompletely = totalWidth <= width(); break;
case Dock::Left:
case Dock::Right:
containsCompletely = totalHeight <= height(); break;
default:
Q_ASSERT(false);
}
// abort adjust.
if (containsCompletely)
return;
// now, we need to decrease item size to fit panel size
int overflow;
int base;
if (m_position == Dock::Top || m_position == Dock::Bottom)
{
qDebug() << "width: " << totalWidth << width();
overflow = totalWidth;
base = width();
}
else
{
qDebug() << "height: " << totalHeight << height();
overflow = totalHeight;
base = height();
}
const int decrease = double(overflow - base) / totalAppItemCount;
int extraDecrease = overflow - base - decrease * totalAppItemCount;
for (auto item : itemList)
{
if (item->itemType() != DockItem::App && item->itemType() != DockItem::Launcher)
continue;
switch (m_position)
{
case Dock::Top:
case Dock::Bottom:
item->setFixedWidth(item->width() - decrease - bool(extraDecrease));
case Dock::Left:
case Dock::Right:
item->setFixedHeight(item->height() - decrease - bool(extraDecrease));
}
if (extraDecrease)
--extraDecrease;
}
// ensure all extra space assigned
Q_ASSERT(extraDecrease == 0);
}
void MainPanel::itemInserted(const int index, DockItem *item)
@ -151,7 +235,7 @@ void MainPanel::itemInserted(const int index, DockItem *item)
initItemConnection(item);
m_itemLayout->insertWidget(index, item);
adjustItemSize();
m_itemAdjustTimer->start();
}
void MainPanel::itemRemoved(DockItem *item)

View File

@ -5,6 +5,7 @@
#include "util/docksettings.h"
#include <QFrame>
#include <QTimer>
#include <QBoxLayout>
class MainPanel : public QFrame
@ -37,6 +38,7 @@ private:
Position m_position;
QBoxLayout *m_itemLayout;
QTimer *m_itemAdjustTimer;
DockItemController *m_itemController;
static DockItem *DragingItem;

View File

@ -233,8 +233,6 @@ void DockSettings::calculateWindowConfig()
}
else if (m_displayMode == Dock::Fashion)
{
// int appItemCount = 0;
// int pluginItemCount = 0;
int perfectWidth = 0;
int perfectHeight = 0;
const QList<DockItem *> itemList = m_itemController->itemList();