annotwindow.cpp 11.6 KB
Newer Older
1
2
/***************************************************************************
 *   Copyright (C) 2006 by Chu Xiaodong <xiaodongchu@gmail.com>            *
3
 *   Copyright (C) 2006 by Pino Toscano <pino@kde.org>                     *
4
5
6
7
8
9
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/
10

Albert Astals Cid's avatar
Albert Astals Cid committed
11
12
#include "annotwindow.h"

13
// qt/kde includes
14
15
16
#include <qapplication.h>
#include <qevent.h>
#include <qfont.h>
17
18
#include <qfontinfo.h>
#include <qfontmetrics.h>
19
#include <qframe.h>
20
21
#include <qlabel.h>
#include <qlayout.h>
22
#include <QPainter>
23
#include <qpushbutton.h>
24
#include <qsizegrip.h>
25
#include <qstyle.h>
26
#include <qtoolbutton.h>
27
28
#include <kglobal.h>
#include <klocale.h>
Albert Astals Cid's avatar
Albert Astals Cid committed
29
#include <ktextedit.h>
30
#include <kdebug.h>
31

32
// local includes
33
#include "core/annotations.h"
34
#include "guiutils.h"
35
36
37
#include "latexrenderer.h"
#include <core/utils.h>
#include <KMessageBox>
38
39
40
41
42
43
44
45
46

class CloseButton
  : public QPushButton
{
public:
    CloseButton( QWidget * parent = 0 )
      : QPushButton( parent )
    {
        setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
47
48
        QSize size = QSize( 14, 14 ).expandedTo( QApplication::globalStrut() );
        setFixedSize( size );
49
        setIcon( style()->standardIcon( QStyle::SP_DockWidgetCloseButton ) );
50
        setIconSize( size );
51
        setToolTip( i18n( "Close this note" ) );
52
53
    }
};
54
55


56
57
class MovableTitle
  : public QWidget
58
{
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public:
    MovableTitle( QWidget * parent )
      : QWidget( parent )
    {
        QVBoxLayout * mainlay = new QVBoxLayout( this );
        mainlay->setMargin( 0 );
        mainlay->setSpacing( 0 );
        // close button row
        QHBoxLayout * buttonlay = new QHBoxLayout();
        mainlay->addLayout( buttonlay );
        titleLabel = new QLabel( this );
        QFont f = titleLabel->font();
        f.setBold( true );
        titleLabel->setFont( f );
        titleLabel->setCursor( Qt::SizeAllCursor );
        buttonlay->addWidget( titleLabel );
        dateLabel = new QLabel( this );
        dateLabel->setAlignment( Qt::AlignTop | Qt::AlignRight );
77
78
79
        f = dateLabel->font();
        f.setPointSize( QFontInfo( f ).pointSize() - 2 );
        dateLabel->setFont( f );
80
81
82
        dateLabel->setCursor( Qt::SizeAllCursor );
        buttonlay->addWidget( dateLabel );
        CloseButton * close = new CloseButton( this );
Laurent Montel's avatar
Laurent Montel committed
83
        connect( close, SIGNAL(clicked()), parent, SLOT(hide()) );
84
85
86
87
88
89
90
91
92
        buttonlay->addWidget( close );
        // option button row
        QHBoxLayout * optionlay = new QHBoxLayout();
        mainlay->addLayout( optionlay );
        authorLabel = new QLabel( this );
        authorLabel->setCursor( Qt::SizeAllCursor );
        authorLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
        optionlay->addWidget( authorLabel );
        optionButton = new QToolButton( this );
93
94
        QString opttext = i18n( "Options" );
        optionButton->setText( opttext );
95
        optionButton->setAutoRaise( true );
96
97
98
        QSize s = QFontMetrics( optionButton->font() ).boundingRect( opttext ).size() + QSize( 8, 8 );
        optionButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
        optionButton->setFixedSize( s );
99
        optionlay->addWidget( optionButton );
100
101
        // ### disabled for now
        optionButton->hide();
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
        latexButton = new QToolButton( this );
        QHBoxLayout * latexlay = new QHBoxLayout();
        QString latextext = i18n ( "This annotation may contain LaTeX code.\nClick here to render." );
        latexButton->setText( latextext );
        latexButton->setAutoRaise( true );
        s = QFontMetrics( latexButton->font() ).boundingRect(0, 0, this->width(), this->height(), 0, latextext ).size() + QSize( 8, 8 );
        latexButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
        latexButton->setFixedSize( s );
        latexButton->setCheckable( true );
        latexButton->setVisible( false );
        latexlay->addSpacing( 1 );
        latexlay->addWidget( latexButton );
        latexlay->addSpacing( 1 );
        mainlay->addLayout( latexlay );
        connect(latexButton, SIGNAL(clicked(bool)), parent, SLOT(renderLatex(bool)));
        connect(parent, SIGNAL(containsLatex(bool)), latexButton, SLOT(setVisible(bool)));
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

        titleLabel->installEventFilter( this );
        dateLabel->installEventFilter( this );
        authorLabel->installEventFilter( this );
    }

    virtual bool eventFilter( QObject * obj, QEvent * e )
    {
        if ( obj != titleLabel && obj != authorLabel && obj != dateLabel )
            return false;

        QMouseEvent * me = 0;
        switch ( e->type() )
        {
            case QEvent::MouseButtonPress:
                me = (QMouseEvent*)e;
                mousePressPos = me->pos();
                break;
            case QEvent::MouseButtonRelease:
                mousePressPos = QPoint();
                break;
            case QEvent::MouseMove:
                me = (QMouseEvent*)e;
                parentWidget()->move( me->pos() - mousePressPos + parentWidget()->pos() );
                break;
            default:
                return false;
        }
        return true;
    }

    void setTitle( const QString& title )
    {
        titleLabel->setText( QString( " " ) + title );
    }

    void setDate( const QDateTime& dt )
    {
156
        dateLabel->setText( KGlobal::locale()->formatDateTime( dt, KLocale::ShortDate, true ) + ' ' );
157
158
159
160
161
162
163
164
165
    }

    void setAuthor( const QString& author )
    {
        authorLabel->setText( QString( " " ) + author );
    }

    void connectOptionButton( QObject * recv, const char* method )
    {
Laurent Montel's avatar
Laurent Montel committed
166
        connect( optionButton, SIGNAL(clicked()), recv, method );
167
168
    }

169
170
171
172
173
    void uncheckLatexButton()
    {
        latexButton->setChecked( false );
    }

174
175
176
177
178
179
private:
    QLabel * titleLabel;
    QLabel * dateLabel;
    QLabel * authorLabel;
    QPoint mousePressPos;
    QToolButton * optionButton;
180
    QToolButton * latexButton;
181
182
183
};


184
// Qt::SubWindow is needed to make QSizeGrip work
185
AnnotWindow::AnnotWindow( QWidget * parent, Okular::Annotation * annot)
186
    : QFrame( parent, Qt::SubWindow ), m_annot( annot )
187
{
188
189
190
    setAutoFillBackground( true );
    setFrameStyle( Panel | Raised );

Pino Toscano's avatar
Pino Toscano committed
191
192
193
    textEdit = new KTextEdit( this );
    textEdit->setAcceptRichText( false );
    textEdit->setPlainText( GuiUtils::contents( m_annot ) );
194
    textEdit->installEventFilter( this );
195
196
    connect(textEdit,SIGNAL(textChanged()),
            this,SLOT(slotsaveWindowText()));
197
198
199
200

    m_latexRenderer = new GuiUtils::LatexRenderer();
    emit containsLatex( GuiUtils::LatexRenderer::mightContainLatex( GuiUtils::contents( m_annot ) ) );

201
202
203
204
205
206
207
208
209
    QVBoxLayout * mainlay = new QVBoxLayout( this );
    mainlay->setMargin( 2 );
    mainlay->setSpacing( 0 );
    m_title = new MovableTitle( this );
    mainlay->addWidget( m_title );
    mainlay->addWidget( textEdit );
    QHBoxLayout * lowerlay = new QHBoxLayout();
    mainlay->addLayout( lowerlay );
    lowerlay->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Fixed ) );
210
    QSizeGrip * sb = new QSizeGrip( this );
211
212
    lowerlay->addWidget( sb );

213
    m_title->setTitle( m_annot->window().summary() );
Laurent Montel's avatar
Laurent Montel committed
214
    m_title->connectOptionButton( this, SLOT(slotOptionBtn()) );
215

216
    setGeometry(10,10,300,300 );
217
218
219
220

    reloadInfo();
}

221
222
223
224
225
AnnotWindow::~AnnotWindow()
{
    delete m_latexRenderer;
}

226
227
228
229
230
231
232
233
234
235
236
void AnnotWindow::reloadInfo()
{
    const QColor newcolor = m_annot->style().color().isValid() ? m_annot->style().color() : Qt::yellow;
    if ( newcolor != m_color )
    {
        m_color = newcolor;
        setPalette( QPalette( m_color ) );
        QPalette pl = textEdit->palette();
        pl.setColor( QPalette::Base, m_color );
        textEdit->setPalette( pl );
    }
Pino Toscano's avatar
Pino Toscano committed
237
    m_title->setAuthor( m_annot->author() );
238
    m_title->setDate( m_annot->modificationDate() );
239
240
}

241
242
243
244
245
246
247
248
void AnnotWindow::showEvent( QShowEvent * event )
{
    QFrame::showEvent( event );

    // focus the content area by default
    textEdit->setFocus();
}

249
250
251
252
253
254
255
256
257
258
259
260
261
262
bool AnnotWindow::eventFilter(QObject *, QEvent *e)
{
    if ( e->type () == QEvent::ShortcutOverride )
    {
        QKeyEvent * keyEvent = static_cast< QKeyEvent * >( e );
        if ( keyEvent->key() == Qt::Key_Escape )
        {
            close();
            return true;
        }
    }
    return false;
}

263
void AnnotWindow::slotOptionBtn()
264
265
266
267
{
    //TODO: call context menu in pageview
    //emit sig...
}
268

269
void AnnotWindow::slotsaveWindowText()
270
{
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    const QString newText = textEdit->toPlainText();

    // 1. window text
    if ( !m_annot->window().text().isEmpty() )
    {
        m_annot->window().setText( newText );
        return;
    }
    // 2. if Text and InPlace, the inplace text
    if ( m_annot->subType() == Okular::Annotation::AText )
    {
        Okular::TextAnnotation * txtann = static_cast< Okular::TextAnnotation * >( m_annot );
        if ( txtann->textType() == Okular::TextAnnotation::InPlace )
        {
            txtann->setInplaceText( newText );
            return;
        }
    }

    // 3. contents
    m_annot->setContents( newText );
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

    emit containsLatex( GuiUtils::LatexRenderer::mightContainLatex( newText ) );
}

void AnnotWindow::renderLatex( bool render )
{
    if (render)
    {
        textEdit->setReadOnly( true );
        disconnect(textEdit, SIGNAL(textChanged()), this,SLOT(slotsaveWindowText()));
        textEdit->setAcceptRichText( true );
        QString contents =  GuiUtils::contents( m_annot );
        contents = Qt::convertFromPlainText( contents );
        QColor fontColor = textEdit->textColor();
        int fontSize = textEdit->fontPointSize();
        QString latexOutput;
        GuiUtils::LatexRenderer::Error errorCode = m_latexRenderer->renderLatexInHtml( contents, fontColor, fontSize, Okular::Utils::dpiX(), latexOutput );
        switch ( errorCode )
        {
            case GuiUtils::LatexRenderer::LatexNotFound:
                KMessageBox::sorry( this, i18n( "Cannot find latex executable." ), i18n( "LaTeX rendering failed" ) );
                m_title->uncheckLatexButton();
                renderLatex( false );
                break;
            case GuiUtils::LatexRenderer::DvipngNotFound:
                KMessageBox::sorry( this, i18n( "Cannot find dvipng executable." ), i18n( "LaTeX rendering failed" ) );
                m_title->uncheckLatexButton();
                renderLatex( false );
                break;
            case GuiUtils::LatexRenderer::LatexFailed:
322
                KMessageBox::detailedSorry( this, i18n( "A problem occurred during the execution of the 'latex' command." ), latexOutput, i18n( "LaTeX rendering failed" ) );
323
324
325
326
                m_title->uncheckLatexButton();
                renderLatex( false );
                break;
            case GuiUtils::LatexRenderer::DvipngFailed:
327
                KMessageBox::sorry( this, i18n( "A problem occurred during the execution of the 'dvipng' command." ), i18n( "LaTeX rendering failed" ) );
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
                m_title->uncheckLatexButton();
                renderLatex( false );
                break;
            case GuiUtils::LatexRenderer::NoError:
            default:
                textEdit->setHtml( contents );
                break;
        }
    }
    else
    {
        textEdit->setAcceptRichText( false );
        textEdit->setPlainText( GuiUtils::contents( m_annot ) );
        connect(textEdit, SIGNAL(textChanged()), this,SLOT(slotsaveWindowText()));
        textEdit->setReadOnly( false );
    }
344
}
345

346
#include "annotwindow.moc"