Mixxx

/home/maxime/Projets/Mixxx/1.10/mixxx/src/library/stareditor.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                            stareditor.cpp
00003                               -------------------
00004      copyright            : (C) 2010 Tobias Rafreider
00005          copyright            : (C) 2009 Nokia Corporation
00006 
00007 ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 /***************************************************************************
00019  *                                                                         *
00020  * StarEditor inherits QWidget and is used by StarDelegate to let the user *
00021  * edit a star rating in the library using the mouse.                      *
00022  *                                                                                                                                                 *
00023  * The class has been adapted from the official "Star Delegate Example",   *
00024  * see http://doc.trolltech.com/4.5/itemviews-stardelegate.html            *
00025  ***************************************************************************/
00026 
00027  #include <QtGui>
00028 
00029  #include "stareditor.h"
00030  #include "starrating.h"
00031 
00032 /*
00033  * We enable mouse tracking on the widget so we can follow the cursor even
00034  * when the user doesn't hold down any mouse button. We also turn on
00035  * QWidget's auto-fill background feature to obtain an opaque background.
00036  * (Without the call, the view's background would shine through the editor.)
00037  */
00038 StarEditor::StarEditor(QWidget *parent, const QStyleOptionViewItem &option)
00039         : QWidget(parent)
00040 {
00041     setPalette(option.palette);
00042     setMouseTracking(true);
00043     setAutoFillBackground(true);
00044 }
00045 
00046 QSize StarEditor::sizeHint() const
00047  {
00048     return m_starRating.sizeHint();
00049  }
00050 /*
00051  * We simply call StarRating::paint() to draw the stars,
00052  * just like we did when implementing StarDelegate
00053  */
00054 void StarEditor::paintEvent(QPaintEvent *)
00055  {
00056     QPainter painter(this);
00057     m_starRating.paint(&painter, rect(), palette(), StarRating::Editable);
00058  }
00059 /*
00060  * In the mouse event handler, we call setStarCount() on
00061  * the private data member m_starRating to reflect the current cursor position,
00062  * and we call QWidget::update() to force a repaint.
00063  */
00064  void StarEditor::mouseMoveEvent(QMouseEvent *event)
00065  {
00066     int star = starAtPosition(event->x());
00067 
00068     if (star != m_starRating.starCount() && star != -1) {
00069        m_starRating.setStarCount(star);
00070        update();
00071     }
00072  }
00073 /*
00074  * When the user releases a mouse button, we simply emit the editingFinished() signal.
00075  */
00076  void StarEditor::mouseReleaseEvent(QMouseEvent * /* event */)
00077  {
00078     emit editingFinished();
00079  }
00080 /*
00081  * The method uses basic linear algebra to find out which star is under the cursor.
00082  */
00083  int StarEditor::starAtPosition(int x)
00084  {
00085      // If the mouse is very close to the left edge, set 0 stars.
00086      if (x < m_starRating.sizeHint().width() * 0.05) {
00087          return 0;
00088      }
00089      int star = (x / (m_starRating.sizeHint().width() / m_starRating.maxStarCount())) + 1;
00090 
00091      if (star <= 0 || star > m_starRating.maxStarCount())
00092          return 0;
00093 
00094      return star;
00095  }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines