tests/manual/qgraphicsitemgroup/widget.cpp
changeset 3 41300fa6a67c
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "widget.h"
       
    43 #include "ui_widget.h"
       
    44 #include <QGraphicsOpacityEffect>
       
    45 #include <QPropertyAnimation>
       
    46 
       
    47 Widget::Widget(QWidget *parent) :
       
    48     QWidget(parent),
       
    49     ui(new Ui::Widget),
       
    50     previousSelectionCount(0)
       
    51 {
       
    52     ui->setupUi(this);
       
    53 
       
    54     effect = new QGraphicsOpacityEffect;
       
    55     effect->setOpacity(0.3);
       
    56     ui->groupBox->setGraphicsEffect(effect);
       
    57 
       
    58     fadeIn = new QPropertyAnimation(effect, "opacity");
       
    59     fadeIn->setDuration(200);
       
    60     fadeIn->setStartValue(0.3);
       
    61     fadeIn->setEndValue(1.);
       
    62 
       
    63     fadeOut = new QPropertyAnimation(effect, "opacity");
       
    64     fadeOut->setDuration(200);
       
    65     fadeOut->setStartValue(1.);
       
    66     fadeOut->setEndValue(0.3);
       
    67 
       
    68     scene = new CustomScene;
       
    69     scene->setSceneRect(-250,-250,500,500);
       
    70     ui->view->setScene(scene);
       
    71     scene->setBackgroundBrush(Qt::white);
       
    72     ui->view->setInteractive(true);
       
    73     ui->view->setDragMode(QGraphicsView::RubberBandDrag);
       
    74     ui->view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
       
    75     ui->view->setRenderHints( QPainter::SmoothPixmapTransform |
       
    76                               QPainter::TextAntialiasing |
       
    77                               QPainter::Antialiasing );
       
    78 
       
    79     rectBlue = new CustomItem(-100, -100, 50, 50, QColor(80, 80, 200));
       
    80     scene->addItem(rectBlue);
       
    81     rectGreen = new CustomItem(100, -100, 50, 50, QColor(80, 200, 80));
       
    82     scene->addItem(rectGreen);
       
    83     rectRed = new CustomItem(-100, 100, 50, 50, QColor(200, 80, 80));
       
    84     scene->addItem(rectRed);
       
    85     rectYellow = new CustomItem(100, 100, 50, 50, QColor(200, 200, 20));
       
    86     scene->addItem(rectYellow);
       
    87 
       
    88     connect(scene, SIGNAL(selectionChanged()), this, SLOT(onSceneSelectionChanged()));
       
    89 }
       
    90 
       
    91 Widget::~Widget()
       
    92 {
       
    93     delete ui;
       
    94     delete fadeIn;
       
    95     delete fadeOut;
       
    96 }
       
    97 
       
    98 void Widget::on_rotate_valueChanged(int value)
       
    99 {
       
   100     if (scene->selectedItems().count() != 1)
       
   101         return;
       
   102 
       
   103     QGraphicsItem *item = scene->selectedItems().at(0);
       
   104     item->setRotation(value);
       
   105     ui->rotateItem->setValue(checkedItem()->rotation());
       
   106 }
       
   107 
       
   108 void Widget::on_scale_valueChanged(int value)
       
   109 {
       
   110     if (scene->selectedItems().count() != 1)
       
   111         return;
       
   112 
       
   113     QGraphicsItem *item = scene->selectedItems().at(0);
       
   114     item->setScale(value / 10.);
       
   115     ui->scaleItem->setValue(checkedItem()->scale() * 10);
       
   116 }
       
   117 
       
   118 void Widget::on_rotateItem_valueChanged(int value)
       
   119 {
       
   120     if (!scene->selectedItems().empty() && scene->selectedItems().at(0) == checkedItem())
       
   121         ui->rotate->setValue(value);
       
   122     else
       
   123         checkedItem()->setRotation(value);
       
   124 }
       
   125 
       
   126 void Widget::on_scaleItem_valueChanged(int value)
       
   127 {
       
   128     if (!scene->selectedItems().empty() && scene->selectedItems().at(0) == checkedItem())
       
   129         ui->scale->setValue(value);
       
   130     else
       
   131         checkedItem()->setScale(value / 10.);
       
   132 }
       
   133 
       
   134 void Widget::on_group_clicked()
       
   135 {
       
   136     QList<QGraphicsItem*> all = scene->selectedItems();
       
   137     if (all.size() < 2)
       
   138         return;
       
   139 
       
   140     QList<CustomItem*> items = scene->selectedCustomItems();
       
   141     QList<CustomGroup*> groups = scene->selectedCustomGroups();
       
   142 
       
   143     if (groups.size() == 1) {
       
   144         foreach (CustomItem *item, items) {
       
   145             item->setSelected(false);
       
   146             groups[0]->addToGroup(item);
       
   147         }
       
   148 
       
   149         return;
       
   150     }
       
   151 
       
   152     CustomGroup* group = new CustomGroup;
       
   153     scene->addItem(group);
       
   154     foreach (QGraphicsItem *item, all) {
       
   155         item->setSelected(false);
       
   156         group->addToGroup(item);
       
   157     }
       
   158     group->setSelected(true);
       
   159 
       
   160     updateUngroupButton();
       
   161 }
       
   162 
       
   163 void Widget::on_dismantle_clicked()
       
   164 {
       
   165     QList<CustomGroup*> groups = scene->selectedCustomGroups();
       
   166 
       
   167     foreach (CustomGroup *group, groups) {
       
   168         foreach (QGraphicsItem *item, group->childItems())
       
   169             group->removeFromGroup(item);
       
   170 
       
   171         delete group;
       
   172     }
       
   173 
       
   174     updateUngroupButton();
       
   175 }
       
   176 
       
   177 void Widget::on_merge_clicked()
       
   178 {
       
   179     QList<CustomGroup*> groups = scene->selectedCustomGroups();
       
   180     if (groups.size() < 2)
       
   181         return;
       
   182 
       
   183     CustomGroup *newBigGroup = groups.takeFirst();
       
   184     foreach (CustomGroup *group, groups) {
       
   185         foreach (QGraphicsItem *item, group->childItems())
       
   186             item->setGroup(newBigGroup);
       
   187 
       
   188         delete group;
       
   189     }
       
   190 }
       
   191 
       
   192 void Widget::onSceneSelectionChanged()
       
   193 {
       
   194     QList<QGraphicsItem*> all = scene->selectedItems();
       
   195     QList<CustomGroup*> groups = scene->selectedCustomGroups();
       
   196 
       
   197     if (all.empty() && all.count() != previousSelectionCount) {
       
   198         fadeOut->start();
       
   199     } else if (previousSelectionCount == 0) {
       
   200         fadeIn->start();
       
   201     }
       
   202 
       
   203     if (all.count() == 1) {
       
   204         QGraphicsItem *item = all.at(0);
       
   205         ui->rotate->setValue(item->rotation());
       
   206         ui->scale->setValue(item->scale() * 10);
       
   207     } else {
       
   208         ui->rotate->setValue(ui->rotate->minimum());
       
   209         ui->scale->setValue(ui->scale->minimum());
       
   210     }
       
   211 
       
   212     ui->rotate->setEnabled(all.size() == 1);
       
   213     ui->scale->setEnabled(all.size() == 1);
       
   214     ui->group->setEnabled(all.size() > 1);
       
   215     ui->dismantle->setEnabled(!groups.empty());
       
   216     ui->merge->setEnabled(groups.size() > 1);
       
   217 
       
   218     previousSelectionCount = all.size();
       
   219 
       
   220     updateUngroupButton();
       
   221 }
       
   222 
       
   223 void Widget::on_ungroup_clicked()
       
   224 {
       
   225     QGraphicsItemGroup *oldGroup = checkedItem()->group();
       
   226     checkedItem()->setGroup(0);
       
   227     if (oldGroup && oldGroup->childItems().empty())
       
   228         delete oldGroup;
       
   229 
       
   230     updateUngroupButton();
       
   231 }
       
   232 
       
   233 void Widget::updateUngroupButton()
       
   234 {
       
   235     ui->ungroup->setEnabled(checkedItem()->group() != 0);
       
   236 }
       
   237 
       
   238 CustomItem * Widget::checkedItem() const
       
   239 {
       
   240     CustomItem *item;
       
   241 
       
   242     if (ui->blue->isChecked())
       
   243         item = rectBlue;
       
   244     else if (ui->red->isChecked())
       
   245         item = rectRed;
       
   246     else if (ui->green->isChecked())
       
   247         item = rectGreen;
       
   248     else if (ui->yellow->isChecked())
       
   249         item = rectYellow;
       
   250 
       
   251     return item;
       
   252 }
       
   253 
       
   254 void Widget::on_buttonGroup_buttonClicked()
       
   255 {
       
   256     ui->rotateItem->setValue(checkedItem()->rotation());
       
   257     ui->scaleItem->setValue(checkedItem()->scale() * 10);
       
   258 
       
   259     updateUngroupButton();
       
   260 }