mainwindow.cpp
author Dmitriy Morozov <dmitriy@mrzv.org>
Mon, 08 Nov 2010 22:45:19 -0800
changeset 1 14e9780340d7
parent 0 7726fefc58a4
child 2 035dc97b8d04
permissions -rw-r--r--
Added IPESOURCE variable to the Makefile

// --------------------------------------------------------------------
// MainWindow
// --------------------------------------------------------------------
/*

    This file is based on ipeview, part of the extensible drawing editor Ipe.
    Copyright (C) 1993-2009  Otfried Cheong
    Copyright (C) 2010       Dmitriy Morozov

    Ipe 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 3 of the License, or
    (at your option) any later version.

    As a special exception, you have permission to link Ipe with the
    CGAL library and distribute executables, as long as you follow the
    requirements of the Gnu General Public License in regard to all of
    the software in the executable aside from CGAL.

    Ipe 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 Ipe; if not, you can find it at
    "http://www.gnu.org/copyleft/gpl.html", or write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "mainwindow.h"
#include "canvasview.h"
#include "ipetool.h"

using namespace ipe;
using namespace ipeqt;

// --------------------------------------------------------------------

MainWindow::
MainWindow(CanvasView* cv, Qt::WFlags f): 
    QMainWindow(0, f), canvas_view(cv)
{
    setupUi(this);

    iDoc = 0;
    page_dialog = 0;

    // iFileMenu = menuBar()->addMenu(tr("&File"));
    // iFileMenu->addAction(new IpeAction(EOpen, "Open", "Ctrl+O", this));
    // iFileMenu->addAction(new IpeAction(EQuit, "Quit", "Ctrl+Q", this));
    
    iViewMenu = menuBar()->addMenu(tr("&View"));
    iMoveMenu = menuBar()->addMenu(tr("&Move"));

    iViewMenu->addAction(new IpeAction(EFullScreen, "Full screen", "F11", this));
    iViewMenu->addAction(new IpeAction(EFitPage, "Fit page", ".", this));
    iViewMenu->addAction(new IpeAction(EZoomIn, "Zoom in", "=", this));
    iViewMenu->addAction(new IpeAction(EZoomOut, "Zoom out", "-", this));
    iViewMenu->addAction(new IpeAction(ESetTime, "Set time", "", this));
    iTimeCountdown = new IpeAction(ETimeCountdown, "Count down", "/", this);
    iTimeCountdown->setCheckable(true);
    iViewMenu->addAction(iTimeCountdown);
    IpeAction* iToggleTime = new IpeAction(EToggleTimeCounting, "Count time", "T", this);
    iToggleTime->setCheckable(true);
    iViewMenu->addAction(iToggleTime);
    iViewMenu->addAction(new IpeAction(EShowPresentation, "Show presentation", "", this));

    iMoveMenu->addAction(new IpeAction(ENextView, "Next view", "PgDown", this));
    iMoveMenu->addAction(new IpeAction(EPreviousView, "Previous view", "PgUp", this));
    iMoveMenu->addAction(new IpeAction(EFirstView, "First view", "Home", this));
    iMoveMenu->addAction(new IpeAction(ELastView, "Last view", "End", this));
    iMoveMenu->addAction(new IpeAction(EJumpTo, "Jump to...", "J", this));

    currentViewCanvas->setPretty(true);
    nextViewCanvas->setPretty(true);

    iSnap.iSnap = 0; // Snap::ESnapGrid;
    iSnap.iGridVisible = false;
    iSnap.iGridSize = 8;
    iSnap.iAngleSize = M_PI / 6.0;
    iSnap.iSnapDistance = 10;
    iSnap.iWithAxes = false;
    iSnap.iOrigin = Vector::ZERO;
    iSnap.iDir = 0;
    currentViewCanvas->setSnap(iSnap);
    nextViewCanvas->setSnap(iSnap);
    // currentViewCanvas->setFifiVisible(true);
    // nextViewCanvas->setFifiVisible(true);

    // connect(currentViewCanvas, SIGNAL(wheelMoved(int)), this, SLOT(wheelZoom(int)));
    // connect(currentViewCanvas, SIGNAL(mouseAction(int)), this, SLOT(startPan(int)));
  
    connect(splitter, SIGNAL(splitterMoved(int,int)), this, SLOT(splitterMoved(int,int)));
    connect(splitter_2, SIGNAL(splitterMoved(int,int)), this, SLOT(splitterMoved(int,int)));

    currentViewCanvas->setFocus();
}

void MainWindow::cmd(int c)
{
    ipeDebug("Command %d", c);
    switch (c) 
    {
        case EOpen:
            break;
        case EQuit:
            QApplication::exit();
            break;
        case EToggleTimeCounting:
            timeLabel->toggleCounting();
            break;
        case EFullScreen:
            canvas_view->setWindowState(canvas_view->windowState() ^ Qt::WindowFullScreen);
            break;
        case EFitPage:
            fitBox(iDoc->cascade()->findLayout()->paper(), currentViewCanvas);
            fitBox(iDoc->cascade()->findLayout()->paper(), nextViewCanvas);
            canvas_view->fitBox(iDoc->cascade()->findLayout()->paper());
            break;
        // case EZoomIn:
        //     zoom(+1);
        //     break;
        // case EZoomOut:
        //     zoom(-1);
        //     break;
        case ENextView:
            nextView(+1);
            break;
        case EPreviousView:
            nextView(-1);
            break;
        case EFirstView:
            firstView();
            break;
        case ELastView:
            lastView();
            break;
        case ETimeCountdown:
            timeLabel->toggleCountdown();
            break;
        case ESetTime:
            timeLabel->setTime();
            break;
        case EJumpTo:
        {
            page_dialog->reset();
            page_dialog->exec();
            if (page_dialog->isOk())
            {
                iPageNo = page_dialog->page();
                iViewNo = 0;
                setView();
            }
            break;
        }
        case EShowPresentation:
            canvas_view->show();
            break;
        default:
            // unknown action
            return;
    }
}

bool MainWindow::load(const char *fname)
{
    Document *doc = Document::loadWithErrorReport(fname);
    if (!doc)
        return false;
    
    iFileName = String(fname);
    doc->runLatex();
    
    delete iDoc;
    iDoc = doc;
    
    iPageNo = 0;
    iViewNo = 0;
    
    // iDoc->page(iPageNo)->setSelect(0, EPrimarySelected);
    currentViewCanvas->setFontPool(iDoc->fontPool());
    nextViewCanvas->setFontPool(iDoc->fontPool());
    
    currentViewCanvas->setPage(iDoc->page(iPageNo), iViewNo, iDoc->cascade());
    currentViewCanvas->setPan(Vector(300, 400));
    currentViewCanvas->update();
    setNextViewCanvas();
    
    canvas_view->load(doc, iPageNo, iViewNo);
     
    delete page_dialog;
    page_dialog = new PageDialog(iDoc);
    
    updateLabel();
    return true;
}
  
void MainWindow::updateLabel()
{
    String s = iFileName;
    if (iFileName.rfind('/') >= 0)
        s = iFileName.substr(iFileName.rfind('/') + 1);
    if (iDoc->countTotalViews() > 1) {
        ipe::StringStream ss(s);
        ss << " (" << iPageNo + 1 << "-" << iViewNo + 1 << ")";
    }
    setWindowTitle(QIpe(s));
}

void MainWindow::fitBox(const Rect &box, Canvas* canvas)
{
    if (box.isEmpty())
        return;
    ipeDebug("canvas: %d x %d", canvas->width(), canvas->height());
    double xfactor = box.width() > 0.0  ? (canvas->width() / box.width()) : 20.0;
    double yfactor = box.height() > 0.0 ? (canvas->height() / box.height()) : 20.0;
    double zoom = (xfactor > yfactor) ? yfactor : xfactor;
    canvas->setPan(0.5 * (box.bottomLeft() + box.topRight()));
    canvas->setZoom(zoom);
    canvas->update();
}

// void MainWindow::zoom(int delta)
// {
//     double zoom = currentViewCanvas->zoom();
//     while (delta > 0) {zoom *= 1.3; --delta;}
//     while (delta < 0) {zoom /= 1.3; ++delta;}
//     currentViewCanvas->setZoom(zoom);
//     currentViewCanvas->update();
// }

// void MainWindow::wheelZoom(int degrees)
// {
//     if (degrees > 0)
//         zoom(+1);
//     else
//         zoom(-1);
// }

// void MainWindow::startPan(int button)
// {
//     Tool *tool = new PanTool(currentViewCanvas, iDoc->page(iPageNo), iViewNo);
//     currentViewCanvas->setTool(tool);
// }

std::pair<int,int> MainWindow::nextPageView(int delta)
{
    const Page *page = iDoc->page(iPageNo);
    int iPageNo = this->iPageNo;
    int iViewNo = this->iViewNo;
    if (0 <= iViewNo + delta && iViewNo + delta < page->countViews()) 
    {
        iViewNo += delta;
    } else if (0 <= iPageNo + delta && iPageNo + delta < iDoc->countPages()) 
    {
        iPageNo += delta;
        if (delta > 0)
            iViewNo = 0;
        else
            iViewNo = iDoc->page(iPageNo)->countViews() - 1;
    } 
    
    return std::make_pair(iPageNo, iViewNo);
}

void MainWindow::setNextViewCanvas()
{
    std::pair<int,int> page_view = nextPageView(1);
    int iPageNo = page_view.first;
    int iViewNo = page_view.second;
    nextViewCanvas->setPage(iDoc->page(iPageNo), iViewNo, iDoc->cascade());
    nextViewCanvas->update();
}

void MainWindow::nextView(int delta)
{
    std::pair<int,int> page_view = nextPageView(delta);
    iPageNo = page_view.first;
    iViewNo = page_view.second;
    setView();
}

void MainWindow::firstView()
{
    iPageNo = 0;
    iViewNo = 0;
    setView();
}

void MainWindow::lastView()
{
    iPageNo = iDoc->countPages() - 1;
    iViewNo = iDoc->page(iPageNo)->countViews() - 1;
    setView();
}

void MainWindow::resizeEvent (QResizeEvent *event)
{
    cmd(EFitPage);
}
  
void MainWindow::splitterMoved(int pos, int index)
{
    cmd(EFitPage);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    canvas_view->close();
    QMainWindow::closeEvent(event);
}

void MainWindow::setView()
{
    currentViewCanvas->setPage(iDoc->page(iPageNo), iViewNo, iDoc->cascade());
    currentViewCanvas->update();
    updateLabel();

    canvas_view->setView(iPageNo, iViewNo);
    
    setNextViewCanvas();
}

// --------------------------------------------------------------------

static void usage()
{
    fprintf(stderr, "Usage: ipepresenter <filename>\n");
    exit(1);
}

int main(int argc, char *argv[])
{
    Platform::initLib(IPELIB_VERSION);
    QApplication a(argc, argv);
    setlocale(LC_NUMERIC, "C");
    
    if (argc != 2)
        usage();
    
    const char *load = argv[1];
    
    CanvasView *cv = new CanvasView();
    MainWindow *mw = new MainWindow(cv);
    
    if (!mw->load(load))
        exit(2);
    
    cv->show();
    mw->show();
    
    mw->cmd(MainWindow::EFitPage);
    
    QObject::connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    return a.exec();
}

// --------------------------------------------------------------------