Skip to content

Commit

Permalink
Overview Fit to Page Width
Browse files Browse the repository at this point in the history
.. resize and scale to fit as the layout changes.
  • Loading branch information
liversedge committed Jan 28, 2017
1 parent 25699d3 commit 5d5e55a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 28 deletions.
78 changes: 52 additions & 26 deletions src/Charts/OverviewWindow.cpp
Expand Up @@ -22,7 +22,7 @@
#include <QGraphicsSceneMouseEvent>

OverviewWindow::OverviewWindow(Context *context) :
GcChartWindow(context), mode(CONFIG), state(NONE), context(context), group(NULL), resizecursor(false)
GcChartWindow(context), mode(CONFIG), state(NONE), context(context), group(NULL), resizecursor(false), block(false)
{
setContentsMargins(0,0,0,0);
setProperty("color", GColor(COVERVIEWBACKGROUND));
Expand Down Expand Up @@ -70,22 +70,11 @@ OverviewWindow::OverviewWindow(Context *context) :
// sort out the view
updateGeometry();

// now scale to fit... nothing fancy for now
view->setSceneRect(scene->itemsBoundingRect());
view->fitInView(view->sceneRect(), Qt::KeepAspectRatio);

// watch the view for mouse events
view->setMouseTracking(true);
scene->installEventFilter(this);
}

void
OverviewWindow::resizeEvent(QResizeEvent *)
{
// hmmm, this isn't quite right !
view->fitInView(view->sceneRect(), Qt::KeepAspectRatio);
}

static bool cardSort(const Card* left, const Card* right)
{
return (left->column < right->column ? true : (left->column == right->column && left->order < right->order ? true : false));
Expand All @@ -100,6 +89,7 @@ OverviewWindow::updateGeometry()
qSort(cards.begin(), cards.end(), cardSort);

int y=70;
int maxy = y;
int column=-1;

// just set their geometry for now, no interaction
Expand All @@ -126,6 +116,9 @@ OverviewWindow::updateGeometry()
int theight = cards[i]->deep * 70;


// for setting the scene rectangle
if (maxy < ty+theight+70) maxy = ty+theight+70;

// add to scene if new
if (!cards[i]->onscene) {
cards[i]->setGeometry(tx, ty, twidth, theight);
Expand All @@ -146,12 +139,15 @@ OverviewWindow::updateGeometry()

// we're starting to animate so clear and restart any animations
if (group) group->clear();
else group = new QParallelAnimationGroup(this);
else {
group = new QParallelAnimationGroup(this);
//connect(group, SIGNAL(finished()), this, SLOT(fitView()));
}
}

// add an animation for this movement
QPropertyAnimation *animation = new QPropertyAnimation(cards[i], "geometry");
animation->setDuration(200);
animation->setDuration(300);
animation->setStartValue(cards[i]->geometry());
animation->setEndValue(QRect(tx,ty,twidth,theight));
animation->setEasingCurve(QEasingCurve(QEasingCurve::OutQuint));
Expand All @@ -163,6 +159,9 @@ OverviewWindow::updateGeometry()
y += theight + 70;
}

// set the scene rectangle, columns start at 0
QRectF rect(0, 0, (column+1) * 870 + 70, maxy);
scene->setSceneRect(rect);
if (animated) group->start();
}

Expand Down Expand Up @@ -199,9 +198,28 @@ OverviewWindow::configChanged(qint32)
repaint();
}

void
OverviewWindow::updateView()
{
scene->update();

// fit to scene width XXX need to fix scrollbars.
double scale = view->frameGeometry().width() / scene->sceneRect().width();
QRectF viewRect(0,0, scene->sceneRect().width(), view->frameGeometry().height() / scale);
view->scale(scale,scale);
view->setSceneRect(viewRect);


view->fitInView(viewRect, Qt::KeepAspectRatio);
view->update();
}

bool
OverviewWindow::eventFilter(QObject *, QEvent *event)
{
if (block) return false;

block = true;
bool returning = false;

// we only filter out keyboard shortcuts for undo redo etc
Expand Down Expand Up @@ -300,12 +318,17 @@ OverviewWindow::eventFilter(QObject *, QEvent *event)

// drop it down
updateGeometry();
scene->update();
view->update();
updateView();
}

} else if (event->type() == QEvent::GraphicsSceneMouseMove) {

// thanks we'll intercept that
if (mode == CONFIG) {
event->accept();
returning = true;
}

if (mode == CONFIG && state == NONE) { // hovering

// where am i ?
Expand Down Expand Up @@ -343,10 +366,6 @@ OverviewWindow::eventFilter(QObject *, QEvent *event)

} else if (mode == CONFIG && state == DRAG) { // dragging?

// we'll take this
event->accept();
returning = true;

// where am i ?
QPointF pos = static_cast<QGraphicsSceneMouseEvent*>(event)->scenePos();

Expand Down Expand Up @@ -385,7 +404,16 @@ OverviewWindow::eventFilter(QObject *, QEvent *event)

// create a new column to the right?
int targetcol = (pos.x()-stateData.drag.offx)/870;
if (cards.last() && cards.last()->column < targetcol) {
if (targetcol < 0) {

// new col to left
for(int i=0; i< cards.count(); i++) cards[i]->column += 1;
stateData.drag.card->column = 0;
stateData.drag.card->order = 0;

} else if (cards.last() && cards.last()->column < targetcol) {

// new col to the right
stateData.drag.card->column = cards.last()->column + 1;
stateData.drag.card->order = 0;

Expand All @@ -407,8 +435,7 @@ OverviewWindow::eventFilter(QObject *, QEvent *event)

// drop it down
updateGeometry();
scene->update();
view->update();
updateView();

} else if (mode == CONFIG && state == RESIZE) {

Expand All @@ -423,12 +450,11 @@ OverviewWindow::eventFilter(QObject *, QEvent *event)

// drop it down
updateGeometry();
scene->update();
view->update();

updateView();
}
}

block = false;
return returning;
}

Expand Down
9 changes: 7 additions & 2 deletions src/Charts/OverviewWindow.h
Expand Up @@ -84,12 +84,16 @@ class OverviewWindow : public GcChartWindow
// trap signals
void configChanged(qint32);

// viewport resized
void resizeEvent(QResizeEvent * event);
// scale on first show
void showEvent(QShowEvent *) { updateView(); }
void resizeEvent(QResizeEvent *) { updateView(); }

// set geometry on the widgets (size and pos)
void updateGeometry();

// set scale, zoom etc appropriately
void updateView();

// create a card
Card *newCard(int column, int order, int deep) { Card *add = new Card(deep);
add->column = column;
Expand All @@ -111,6 +115,7 @@ class OverviewWindow : public GcChartWindow
QGraphicsView *view;
QParallelAnimationGroup *group;
bool resizecursor; // is the cursor set to resize?
bool block; // block event processing

// content
QList<Card*> cards;
Expand Down

0 comments on commit 5d5e55a

Please sign in to comment.