summaryrefslogtreecommitdiffstats
path: root/kpresenter/KPrNoteBar.cpp
blob: 9548d259ce520720d245af1ac599f80b96b0abae (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 <tqlayout.h>
#include <tqpainter.h>
#include <tqpaintdevicemetrics.h>
#include <tqsimplerichtext.h>
#include <tqlabel.h>

#include <tdeglobalsettings.h>
#include <kprinter.h>
#include <kdebug.h>
#include <ktextedit.h>
#include <tdelocale.h>

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


KPrNoteBar::KPrNoteBar( TQWidget *_parent, KPrView *_view )
    : TQWidget( _parent ),
      view( _view ),
      initialize( true )
{
    TQBoxLayout *topLayout = new TQVBoxLayout( this, KDialog::marginHint(), KDialog::spacingHint() );

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

    textEdit = new KTextEdit( this );

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

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

    connect( textEdit, TQT_SIGNAL( textChanged() ),
             this, TQT_SLOT( slotTextChanged() ) );

    connect( textEdit, TQT_SIGNAL( selectionChanged() ),
             this, TQT_SLOT( slotSelectionChanged() ) );

    connect( textEdit, TQT_SIGNAL( copyAvailable( bool ) ),
             this, TQT_SLOT( slotSelectionChanged() ) );

    connect( textEdit, TQT_SIGNAL( undoAvailable( bool ) ),
             this, TQT_SLOT( slotUndoAvailable( bool ) ) );

    connect( textEdit, TQT_SIGNAL( redoAvailable( bool ) ),
             this, TQT_SLOT( slotRedoAvailable( bool ) ) );

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

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

void KPrNoteBar::setCurrentNoteText( const TQString &_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( TQPainter *_painter, KPrinter *_printer, TQValueList<int> _list )
{
    // base code from $TQTDIR/example/textedit/textedit.cpp
    _painter->save();

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

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

    TQFont font = KoGlobal::defaultFont();
    TQString allText = getNotesTextForPrinting(_list);
    TQString str = TQStyleSheet::convertFromPlainText( allText );

    TQSimpleRichText richText( str, font, TQString(), TQStyleSheet::defaultSheet(),
                              TQMimeSourceFactory::defaultFactory(), body.height() );

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

    TQRect 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();
}

TQString KPrNoteBar::getNotesTextForPrinting(TQValueList<int> _list) const
{
    TQString allText = TQString();
    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 += TQString("\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 += TQString("\n\n");
    allText += i18n( "Master Page Note:\n" );
    if ( !doc->masterPage()->noteText().isEmpty() )
         noteIsEmpty = false;
    allText += doc->masterPage()->noteText();

    if( noteIsEmpty )
        return TQString();
    return allText;
}

#include "KPrNoteBar.moc"