//=========================================================
//  MusE
//  Linux Music Editor
//    $Id: tscale.cpp,v 1.2 2003/12/17 11:04:14 wschweer Exp $
//  (C) Copyright 1999 Werner Schweer (ws@seh.de)
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; version 2 of
//  the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//=========================================================

#include <stdio.h>
#include "tscale.h"
#include "globals.h"
#include "gconfig.h"

#include <QMouseEvent>
#include <QPainter>
#include <QStyleOption>

namespace MusEGui {

//---------------------------------------------------------
//   TScale
//---------------------------------------------------------

TScale::TScale(QWidget* parent, int ymag)
   : View(parent, 1, ymag)
      {
      setFont(MusEGlobal::config.fonts[5]);
// Width() is obsolete. Qt >= 5.11 use horizontalAdvance().
#if QT_VERSION >= 0x050b00
      int w = 4 * fontMetrics().horizontalAdvance('0');
#else
      int w = 4 * fontMetrics().width('0');
#endif
      setFixedWidth(w);
      setMouseTracking(true);
      }

//---------------------------------------------------------
//   draw
//---------------------------------------------------------

void TScale::pdraw(QPainter& p, const QRect& r, const QRegion&)
      {
    QStyleOption opt;
    opt.init(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

      int y = r.y();
      int h = r.height();
      QString s;
      p.setPen(palette().color(QPalette::WindowText));
      for (int i = 30000; i <= 250000; i += 10000) {
            int yy =  mapy(280000 - i);
            if (yy < y)
                  break;
            if (yy-15 > y+h)
                  continue;
            p.drawLine(0, yy, width(), yy);
            s.setNum(i/1000);
// Width() is obsolete. Qt >= 5.11 use horizontalAdvance().
#if QT_VERSION >= 0x050b00
            p.drawText(width() - fontMetrics().horizontalAdvance(s) - 1, yy-2, s);  // Use the window font. Tim p4.0.31
#else
            p.drawText(width() - fontMetrics().width(s) - 1, yy-2, s);  // Use the window font. Tim p4.0.31
#endif
            }
      }

void TScale::viewMouseMoveEvent(QMouseEvent* event)
      {
      emit tempoChanged(280000 - event->y());
      }

void TScale::leaveEvent(QEvent*)
      {
      emit tempoChanged(-1);
      }

} // namespace MusEGui
