summaryrefslogtreecommitdiffstats
path: root/kpresenter/KPrNoteBar.cpp
blob: f44865f1fd3d1034c592e16f97c534776c58ba51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
/* This file is part of the KDE project
   Copyright (C) 2001 Toshitaka Fujioka <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/


#include <qlayout.h>
#include <qpainter.h>
#include <qpaintdevicemetrics.h>
#include <qsimplerichtext.h>
#include <qlabel.h>

#include <kglobalsettings.h>
#include <kprinter.h>
#include <kdebug.h>
#include <ktextedit.h>
#include <klocale.h>

#include "KPrNoteBar.h"
#include "KPrView.h"
#include "KPrDocument.h"
#include "KPrPage.h"


KPrNoteBar::KPrNoteBar( QWidget *_parent, KPrView *_view )
    : QWidget( _parent ),
      view( _view ),
      initialize( true )
{
    QBoxLayout *topLayout = new QVBoxLayout( this, KDialog::marginHint(), KDialog::spacingHint() );

    label = new QLabel(i18n("Note"), this);

    textEdit = new KTextEdit( this );

    QFont font = KoGlobal::defaultFont();
    textEdit->setCurrentFont( font );

    int currentPageNum = view->getCurrentPresPage(); // 1 base.
    QString text=QString::null;
    if (currentPageNum!=-1)
        text = view->kPresenterDoc()->pageList().at(currentPageNum - 1)->noteText( );
    textEdit->setText( text );

    connect( textEdit, SIGNAL( textChanged() ),
             this, SLOT( slotTextChanged() ) );

    connect( textEdit, SIGNAL( selectionChanged() ),
             this, SLOT( slotSelectionChanged() ) );

    connect( textEdit, SIGNAL( copyAvailable( bool ) ),
             this, SLOT( slotSelectionChanged() ) );

    connect( textEdit, SIGNAL( undoAvailable( bool ) ),
             this, SLOT( slotUndoAvailable( bool ) ) );

    connect( textEdit, SIGNAL( redoAvailable( bool ) ),
             this, SLOT( slotRedoAvailable( bool ) ) );

    topLayout->addWidget( label );
    topLayout->addWidget( textEdit );
}

KPrNoteBar::~KPrNoteBar()
{
    delete textEdit;
}

void KPrNoteBar::setCurrentNoteText( const QString &_text )
{
    initialize = true;
    textEdit->setText( _text );
    initialize = false;
}

void KPrNoteBar::slotTextChanged()
{
    int currentPageNum = view->getCurrPgNum(); // 1 base.
    if ( currentPageNum > 0 && !initialize ) {
        if ( view->editMaster() )
            view->kPresenterDoc()->refreshAllNoteBarMasterPage(textEdit->text() , view);
        else
            view->kPresenterDoc()->refreshAllNoteBar(currentPageNum -1,textEdit->text() , view);
        textEdit->setModified( true );
    }
}

void KPrNoteBar::slotSelectionChanged()
{
    kdDebug(33001) << "slotSelectionChanged(): " << textEdit->hasSelectedText() << endl;
}

void KPrNoteBar::slotCopyAvailable( bool yes )
{
    kdDebug(33001) << "slotCopyAvailable( " << yes << " )" << endl;
}

void KPrNoteBar::slotUndoAvailable( bool /*yes*/ )
{
    //kdDebug(33001) << "slotUndoAvailable( " << yes << " )" << endl;
}

void KPrNoteBar::slotRedoAvailable( bool /*yes*/ )
{
    //kdDebug(33001) << "slotRedoAvailable( " << yes << " )" << endl;
}

void KPrNoteBar::printNotes( QPainter *_painter, KPrinter *_printer, QValueList<int> _list )
{
    // base code from $QTDIR/example/textedit/textedit.cpp
    _painter->save();

    QPaintDeviceMetrics metrics( _painter->device() );
    int dpix = metrics.logicalDpiX();
    int dpiy = metrics.logicalDpiY();

    const int margin = 72; // pt
    QRect body( margin * dpix / 72, margin * dpiy / 72,
                metrics.width() - margin * dpix / 72 * 2,
                metrics.height() - margin * dpiy / 72 * 2 );

    QFont font = KoGlobal::defaultFont();
    QString allText = getNotesTextForPrinting(_list);
    QString str = QStyleSheet::convertFromPlainText( allText );

    QSimpleRichText richText( str, font, QString::null, QStyleSheet::defaultSheet(),
                              QMimeSourceFactory::defaultFactory(), body.height() );

    richText.setWidth( _painter, body.width() );

    QRect viewRect( body );
    do {
        richText.draw( _painter, body.left(), body.top(), viewRect, colorGroup() );
        viewRect.moveBy( 0, body.height() );
        _painter->translate( 0, -body.height() );
        _painter->setFont( font );

        if ( viewRect.top() >= richText.height() )
            break;

        _printer->newPage();
    } while ( true );

    _painter->restore();
}

QString KPrNoteBar::getNotesTextForPrinting(QValueList<int> _list) const
{
    QString allText = QString::null;
    bool firstText = true;
    bool noteIsEmpty = true;
    int pageCount = 1;
    KPrDocument *doc=view->kPresenterDoc();
    for ( int i = 0; i < static_cast<int>( doc->pageList().count() ); i++, ++pageCount )
    {
        if (_list.contains(i+1)==0) // that slide isn't printed, don't print its note either
            continue;

        if ( !firstText )
            allText += QString("\n\n");

        allText += i18n( "Slide Note %1:\n" ).arg( pageCount );
        if(noteIsEmpty && !doc->pageList().at(i)->noteText().isEmpty())
            noteIsEmpty = false;
        allText += doc->pageList().at(i)->noteText();

        firstText = false;
    }
    //code for master page
    if ( !firstText )
        allText += QString("\n\n");
    allText += i18n( "Master Page Note:\n" );
    if ( !doc->masterPage()->noteText().isEmpty() )
         noteIsEmpty = false;
    allText += doc->masterPage()->noteText();

    if( noteIsEmpty )
        return QString::null;
    return allText;
}

#include "KPrNoteBar.moc"