tools/shared/qtgradienteditor/qtgradientdialog.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 tools applications 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 "qtgradientdialog.h"
       
    43 #include "ui_qtgradientdialog.h"
       
    44 #include <QtGui/QPushButton>
       
    45 
       
    46 QT_BEGIN_NAMESPACE
       
    47 
       
    48 class QtGradientDialogPrivate
       
    49 {
       
    50     QtGradientDialog *q_ptr;
       
    51     Q_DECLARE_PUBLIC(QtGradientDialog)
       
    52 public:
       
    53 
       
    54     void slotAboutToShowDetails(bool details, int extensionWidthHint);
       
    55 
       
    56     Ui::QtGradientDialog m_ui;
       
    57 };
       
    58 
       
    59 void QtGradientDialogPrivate::slotAboutToShowDetails(bool details, int extensionWidthHint)
       
    60 {
       
    61     if (details) {
       
    62         q_ptr->resize(q_ptr->size() + QSize(extensionWidthHint, 0));
       
    63     } else {
       
    64         q_ptr->setMinimumSize(1, 1);
       
    65         q_ptr->resize(q_ptr->size() - QSize(extensionWidthHint, 0));
       
    66         q_ptr->setMinimumSize(0, 0);
       
    67     }
       
    68 }
       
    69 
       
    70 /*!
       
    71     \class QtGradientDialog
       
    72 
       
    73     \brief The QtGradientDialog class provides a dialog for specifying gradients.
       
    74 
       
    75     The gradient dialog's function is to allow users to edit gradients.
       
    76     For example, you might use this in a drawing program to allow the user to set the brush gradient.
       
    77 
       
    78     \table
       
    79     \row
       
    80     \o \inlineimage qtgradientdialog.png
       
    81     \o \inlineimage qtgradientdialogextension.png
       
    82     \header
       
    83     \o Details extension hidden
       
    84     \o Details extension visible
       
    85     \endtable
       
    86 
       
    87     Starting from the top of the dialog there are several buttons:
       
    88 
       
    89     \image qtgradientdialogtopbuttons.png
       
    90 
       
    91     The first three buttons allow for changing a type of the gradient (QGradient::Type), while the second three allow for
       
    92     changing spread of the gradient (QGradient::Spread). The last button shows or hides the details extension of the dialog.
       
    93     Conceptually the default view with hidden details provides the full functional control over gradient editing.
       
    94     The additional extension with details allows to set gradient's parameters more precisely. The visibility
       
    95     of extension can be controlled by detailsVisible property. Moreover, if you don't want the user to
       
    96     switch on or off the visibility of extension you can set the detailsButtonVisible property to false.
       
    97 
       
    98     Below top buttons there is an area where edited gradient is interactively previewed.
       
    99     In addition the user can edit gradient type's specific parameters directly in this area by dragging
       
   100     appropriate handles.
       
   101 
       
   102     \table
       
   103     \row
       
   104     \o \inlineimage qtgradientdialoglineareditor.png
       
   105     \o \inlineimage qtgradientdialogradialeditor.png
       
   106     \o \inlineimage qtgradientdialogconicaleditor.png
       
   107     \header
       
   108     \o Editing linear type
       
   109     \o Editing radial type
       
   110     \o Editing conical type
       
   111     \row
       
   112     \o The user can change the start and final point positions by dragging the circular handles.
       
   113     \o The user can change the center and focal point positions by dragging the circular handles
       
   114         and can change the gradient's radius by dragging horizontal or vertical line.
       
   115     \o The user can change the center point by dragging the circular handle
       
   116         and can change the gradient's angle by dragging the big wheel.
       
   117     \endtable
       
   118 
       
   119     In the middle of the dialog there is an area where the user can edit gradient stops.
       
   120 
       
   121     \table
       
   122     \row
       
   123     \o \inlineimage qtgradientdialogstops.png
       
   124     \o \inlineimage qtgradientdialogstopszoomed.png
       
   125     \endtable
       
   126 
       
   127     The top part of this area contains stop handles, and bottom part shows the preview of gradient stops path.
       
   128     In order to create a new gradient stop double click inside the view over the desired position.
       
   129     If you double click on existing stop handle in the top part of the view, clicked handle will be duplicated
       
   130     (duplicate will contain the same color).
       
   131     The stop can be activated by clicking on its handle. You can activate previous or next stop by pressing
       
   132     left or right key respectively. To jump to the first or last stop press home or end key respectively.
       
   133     The gradient stops editor supports multiselection.
       
   134     Clicking a handle holding the shift modifier key down will select a range of stops between
       
   135     the active stop and clicked one. Clicking a handle holding control modifier key down will remove from or
       
   136     add to selection the clicked stop depending if it was or wasn't already selected respectively.
       
   137     Multiselection can also be created using rubberband (by pressing the left mouse button outside
       
   138     of any handle and dragging).
       
   139     Sometimes it's hard to select a stop because its handle can be partially covered by other handle.
       
   140     In that case the user can zoom in the view by spinning mouse wheel.
       
   141     The selected stop handles can be moved by drag & drop. In order to remove selected stops press delete key.
       
   142     For convenience context menu is provided with the following actions:
       
   143 
       
   144     \list
       
   145     \o New Stop - creates a new gradient stop
       
   146     \o Delete - removes the active and all selected stops
       
   147     \o Flip All - mirrors all stops
       
   148     \o Select All - selects all stops
       
   149     \o Zoom In - zooms in
       
   150     \o Zoom Out - zooms out
       
   151     \o Zoom All - goes back to original 100% zoom
       
   152     \endlist
       
   153 
       
   154     The bottom part of the QtGradientDialog contains a set of widgets allowing to control the color of
       
   155     the active and selected stops.
       
   156 
       
   157     \table
       
   158     \row
       
   159     \o \inlineimage qtgradientdialogcolorhsv.png
       
   160     \o \inlineimage qtgradientdialogcolorrgb.png
       
   161     \endtable
       
   162 
       
   163 
       
   164     The color button shows the color of the active gradient stop. It also allows for choosing
       
   165     a color from standard color dialog and applying it to the
       
   166     active stop and all selected stops. It's also possible to drag a color directly from the color button
       
   167     and to drop it in gradient stops editor at desired position (it will create new stop with dragged color)
       
   168     or at desired stop handle (it will change the color of that handle).
       
   169 
       
   170     To the right of color button there is a set of 2 radio buttons which allows to switch between
       
   171     HVS and RGB color spec.
       
   172 
       
   173     Finally there are 4 color sliders working either in HSVA (hue saturation value alpha) or
       
   174     RGBA (red green blue alpha) mode, depending on which radio button is chosen. The radio buttons
       
   175     can be controlled programatically by spec() and setSpec() methods. The sliders show the
       
   176     color of the active stop. By double clicking inside color slider you can set directly the desired color.
       
   177     Changes of slider's are applied to stop selection in the way that the color
       
   178     component being changed is applied to stops in selection only, while other components
       
   179     remain unchanged in selected stops (e.g. when the user is changing the saturation,
       
   180     new saturation is applied to selected stops preventing original hue, value and alpha in multiselection).
       
   181 
       
   182     The convenient static functions getGradient() provide modal gradient dialogs, e.g.:
       
   183 
       
   184     \snippet doc/src/snippets/code/tools_shared_qtgradienteditor_qtgradientdialog.cpp 0
       
   185 
       
   186     In order to have more control over the properties of QtGradientDialog use
       
   187     standard QDialog::exec() method:
       
   188 
       
   189     \snippet doc/src/snippets/code/tools_shared_qtgradienteditor_qtgradientdialog.cpp 1
       
   190 
       
   191     \sa {Gradient View Example}
       
   192 */
       
   193 
       
   194 /*!
       
   195     Constructs a gradient dialog with \a parent as parent widget.
       
   196 */
       
   197 
       
   198 QtGradientDialog::QtGradientDialog(QWidget *parent)
       
   199     : QDialog(parent), d_ptr(new QtGradientDialogPrivate())
       
   200 {
       
   201 //    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
       
   202     d_ptr->q_ptr = this;
       
   203     d_ptr->m_ui.setupUi(this);
       
   204     QPushButton *button = d_ptr->m_ui.buttonBox->button(QDialogButtonBox::Ok);
       
   205     if (button)
       
   206         button->setAutoDefault(false);
       
   207     button = d_ptr->m_ui.buttonBox->button(QDialogButtonBox::Cancel);
       
   208     if (button)
       
   209         button->setAutoDefault(false);
       
   210     connect(d_ptr->m_ui.gradientEditor, SIGNAL(aboutToShowDetails(bool, int)),
       
   211                 this, SLOT(slotAboutToShowDetails(bool, int)));
       
   212 }
       
   213 
       
   214 /*!
       
   215     Destroys the gradient dialog
       
   216 */
       
   217 
       
   218 QtGradientDialog::~QtGradientDialog()
       
   219 {
       
   220 }
       
   221 
       
   222 /*!
       
   223     \property QtGradientDialog::gradient
       
   224     \brief the gradient of the dialog
       
   225 */
       
   226 void QtGradientDialog::setGradient(const QGradient &gradient)
       
   227 {
       
   228     d_ptr->m_ui.gradientEditor->setGradient(gradient);
       
   229 }
       
   230 
       
   231 QGradient QtGradientDialog::gradient() const
       
   232 {
       
   233     return d_ptr->m_ui.gradientEditor->gradient();
       
   234 }
       
   235 
       
   236 /*!
       
   237     \property QtGradientDialog::backgroundCheckered
       
   238     \brief whether the background of widgets able to show the colors with alpha channel is checkered.
       
   239 
       
   240     \table
       
   241     \row
       
   242     \o \inlineimage qtgradientdialogbackgroundcheckered.png
       
   243     \o \inlineimage qtgradientdialogbackgroundtransparent.png
       
   244     \row
       
   245     \o \snippet doc/src/snippets/code/tools_shared_qtgradienteditor_qtgradientdialog.cpp 2
       
   246     \o \snippet doc/src/snippets/code/tools_shared_qtgradienteditor_qtgradientdialog.cpp 3
       
   247     \endtable
       
   248 
       
   249     When this property is set to true (the default) widgets inside gradient dialog like color button,
       
   250     color sliders, gradient stops editor and gradient editor will show checkered background
       
   251     in case of transparent colors. Otherwise the background of these widgets is transparent.
       
   252 */
       
   253 
       
   254 bool QtGradientDialog::isBackgroundCheckered() const
       
   255 {
       
   256     return d_ptr->m_ui.gradientEditor->isBackgroundCheckered();
       
   257 }
       
   258 
       
   259 void QtGradientDialog::setBackgroundCheckered(bool checkered)
       
   260 {
       
   261     d_ptr->m_ui.gradientEditor->setBackgroundCheckered(checkered);
       
   262 }
       
   263 
       
   264 /*!
       
   265     \property QtGradientDialog::detailsVisible
       
   266     \brief whether details extension is visible.
       
   267 
       
   268     When this property is set to true the details extension is visible. By default
       
   269     this property is set to false and the details extension is hidden.
       
   270 
       
   271     \sa detailsButtonVisible
       
   272 */
       
   273 bool QtGradientDialog::detailsVisible() const
       
   274 {
       
   275     return d_ptr->m_ui.gradientEditor->detailsVisible();
       
   276 }
       
   277 
       
   278 void QtGradientDialog::setDetailsVisible(bool visible)
       
   279 {
       
   280     d_ptr->m_ui.gradientEditor->setDetailsVisible(visible);
       
   281 }
       
   282 
       
   283 /*!
       
   284     \property QtGradientDialog::detailsButtonVisible
       
   285     \brief whether the details button allowing for showing and hiding details extension is visible.
       
   286 
       
   287     When this property is set to true (the default) the details button is visible and the user
       
   288     can show and hide details extension interactively. Otherwise the button is hidden and the details
       
   289     extension is always visible or hidded depending on the value of detailsVisible property.
       
   290 
       
   291     \sa detailsVisible
       
   292 */
       
   293 bool QtGradientDialog::isDetailsButtonVisible() const
       
   294 {
       
   295     return d_ptr->m_ui.gradientEditor->isDetailsButtonVisible();
       
   296 }
       
   297 
       
   298 void QtGradientDialog::setDetailsButtonVisible(bool visible)
       
   299 {
       
   300     d_ptr->m_ui.gradientEditor->setDetailsButtonVisible(visible);
       
   301 }
       
   302 
       
   303 /*!
       
   304     Returns the current QColor::Spec used for the color sliders in the dialog.
       
   305 */
       
   306 QColor::Spec QtGradientDialog::spec() const
       
   307 {
       
   308     return d_ptr->m_ui.gradientEditor->spec();
       
   309 }
       
   310 
       
   311 /*!
       
   312     Sets the current QColor::Spec to \a spec used for the color sliders in the dialog.
       
   313 */
       
   314 void QtGradientDialog::setSpec(QColor::Spec spec)
       
   315 {
       
   316     d_ptr->m_ui.gradientEditor->setSpec(spec);
       
   317 }
       
   318 
       
   319 /*!
       
   320     Executes a modal gradient dialog, lets the user to specify a gradient, and returns that gradient.
       
   321 
       
   322     If the user clicks \gui OK, the gradient specified by the user is returned. If the user clicks \gui Cancel, the \a initial gradient is returned.
       
   323 
       
   324     The dialog is constructed with the given \a parent. \a caption is shown as the window title of the dialog and
       
   325     \a initial is the initial gradient shown in the dialog. If the \a ok parameter is not-null,
       
   326     the value it refers to is set to true if the user clicks \gui OK, and set to false if the user clicks \gui Cancel.
       
   327 */
       
   328 QGradient QtGradientDialog::getGradient(bool *ok, const QGradient &initial, QWidget *parent, const QString &caption)
       
   329 {
       
   330     QtGradientDialog dlg(parent);
       
   331     if (!caption.isEmpty())
       
   332         dlg.setWindowTitle(caption);
       
   333     dlg.setGradient(initial);
       
   334     const int res = dlg.exec();
       
   335     if (ok) {
       
   336         *ok = (res == QDialog::Accepted) ? true : false;
       
   337     }
       
   338     if (res == QDialog::Accepted)
       
   339         return dlg.gradient();
       
   340     return initial;
       
   341 }
       
   342 
       
   343 /*!
       
   344     This method calls getGradient(ok, QLinearGradient(), parent, caption).
       
   345 */
       
   346 QGradient QtGradientDialog::getGradient(bool *ok, QWidget *parent, const QString &caption)
       
   347 {
       
   348     return getGradient(ok, QLinearGradient(), parent, caption);
       
   349 }
       
   350 
       
   351 QT_END_NAMESPACE
       
   352 
       
   353 #include "moc_qtgradientdialog.cpp"