/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
    Copyright 2000-2026 the Rosegarden development team.

    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.

    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; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#define RG_MODULE_STRING "[StandardRuler]"
#define RG_NO_DEBUG_PRINT

#include "StandardRuler.h"

#include "LoopRuler.h"
#include "MarkerRuler.h"

#include "misc/Debug.h"
#include "base/RulerScale.h"
#include "document/RosegardenDocument.h"
#include "document/CommandHistory.h"
#include "gui/application/CompositionPosition.h"
#include "gui/application/RosegardenMainWindow.h"
#include "gui/general/GUIPalette.h"
#include "document/RosegardenDocument.h"

#include <QObject>
#include <QToolTip>
#include <QWidget>
#include <QVBoxLayout>


namespace Rosegarden
{


StandardRuler::StandardRuler(RosegardenDocument *doc,
                             RulerScale *rulerScale,
                             bool invert,
                             bool isForMainWindow,
                             QWidget *parent) :
    QWidget(parent),
    m_invert(invert),
    m_currentXOffset(0),
    m_doc(doc),
    m_rulerScale(rulerScale)
{
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    setLayout(layout);

    if (!m_invert) {
        m_markerRuler = new MarkerRuler
                       (m_doc, m_rulerScale, this);
        layout->addWidget(m_markerRuler);
    }

    m_loopRuler = new LoopRuler(
            m_doc,
            m_rulerScale,
            15,  // height, 15 is the same height as the MarkerRuler
            m_invert,
            isForMainWindow,  // displayQuickMarker
            this);  // parent
    layout->addWidget(m_loopRuler);

    if (m_invert) {
        m_markerRuler = new MarkerRuler
                       (m_doc, m_rulerScale, this);
        layout->addWidget(m_markerRuler);
    }

    connect(CommandHistory::getInstance(),
            &CommandHistory::commandExecuted,
            this,
            static_cast<void(StandardRuler::*)()>(&StandardRuler::update));

    if (RosegardenMainWindow::self()) {
        QObject::connect(m_loopRuler,
                         &LoopRuler::setPlayPosition,
                         RosegardenMainWindow::self(),
                         &RosegardenMainWindow::slotSetPlayPosition);
    }
}

void StandardRuler::setSnapGrid(const SnapGrid *grid)
{
    m_loopRuler->setSnapGrid(grid);
}

void StandardRuler::setAutoScroller(QPointer<AutoScroller> autoScroller)
{
    if (m_markerRuler)
        m_markerRuler->setAutoScroller(autoScroller);
    if (m_loopRuler)
        m_loopRuler->setAutoScroller(autoScroller);
}

void StandardRuler::setMainWindow(QPointer<QMainWindow> mainWindow)
{
    if (m_markerRuler)
        m_markerRuler->setMainWindow(mainWindow);
    if (m_loopRuler)
        m_loopRuler->setMainWindow(mainWindow);
}

void StandardRuler::setDocument(RosegardenDocument*)
{
    //RG_DEBUG << "connectRulerToDocPointer()";

    Q_ASSERT(m_loopRuler);
    Q_ASSERT(m_markerRuler);

    // use the document as a hub for pointer and loop set related signals
    // pointer and loop drag signals are specific to the current view,
    // so they are re-emitted from the loop ruler by this widget
    //
    QObject::connect(
            m_loopRuler, &LoopRuler::setPointerPosition,
            CompositionPosition::getInstance(), &CompositionPosition::slotSet);

    QObject::connect
    (m_loopRuler, &LoopRuler::dragPointerToPosition,
     this, &StandardRuler::dragPointerToPosition);

//    m_loopRuler->setBackgroundColor(GUIPalette::getColour(GUIPalette::PointerRuler));
}

void StandardRuler::slotScrollHoriz(int x)
{
    m_loopRuler->scrollHoriz(x);
    m_markerRuler->scrollHoriz(x);
}

void StandardRuler::setMinimumWidth(int width)
{
    m_markerRuler->setMinimumWidth(width);
    m_loopRuler->setMinimumWidth(width);
}

void StandardRuler::updateStandardRuler()
{
    m_markerRuler->update();
    m_loopRuler->update();
}

}
