cursorKpart.patch

Patch to update KPart ot take advantage of Cursor callbacks. - Donald Curtis, 06/28/2008 02:46 AM

Download (19.4 KB)

 
b/kpart_yzis/kycursor.cpp Fri Jun 27 16:47:07 2008 -0500
27 27

  
28 28
#include <QPainter>
29 29

  
30
KYCursor::KYCursor( KYEditor* parent, shape type )
31
        : QWidget( parent )
30
#include "debug.h"
31

  
32
#define deepdbg() yzDeepDebug("KYCursor")
33
#define dbg() yzDebug("KYCursor")
34
#define err() yzError("KYCursor")
35

  
36
KYCursor::KYCursor( KYEditor* editor, YViewIface::CursorShape shape )
37
        : QWidget( editor )
32 38
{
39
	mEditor = editor;
33 40
    move( 0, 0 );
34
    setCursorType( type );
41
    setCursorShape( shape );
35 42
}
36 43

  
37 44
KYCursor::~KYCursor()
38 45
{}
39 46

  
40
KYCursor::shape KYCursor::type() const
47
YViewIface::CursorShape KYCursor::shape() const
41 48
{
42
    return mCursorType;
49
    return mCursorShape;
43 50
}
44
void KYCursor::setCursorType( shape type )
51
void KYCursor::setCursorShape( YViewIface::CursorShape shape )
45 52
{
46
    if ( type == mCursorType )
53
    if ( shape == mCursorShape )
47 54
        return ;
48
    mCursorType = type;
55
    mCursorShape = shape;
49 56
    int w = parentWidget()->fontMetrics().maxWidth();
50 57
    int h = parentWidget()->fontMetrics().lineSpacing();
51
    if ( mCursorType == VBAR )
58
    if ( mCursorShape == YViewIface::CursorVbar )
52 59
        w = 2;
53 60
    resize( w, h );
54 61
}
55 62

  
56 63
void KYCursor::paintEvent( QPaintEvent* )
57 64
{
65
    //const YDrawCell cell( mView->m_drawBuffer.at( mEdit->translateRealToPosition(pos()) ) );
66
	const YDrawCell cell( mEditor->view()->getCursorDrawCell() );
67
    QColor cbg, cfg;
68

  
69
    deepdbg().SPrintf( "paintEvent(): cell string='%s'", qp(cell.c) );
70

  
71
    if (cell.bg.isValid()) {
72
        deepdbg() << "paintEvent(): valid cell bg" << endl;
73
        cbg = QColor(cell.bg.rgb());
74
        //cbg = QColor( Qt::red );
75
    } else {
76
        deepdbg() << "paintEvent(): invalid cell bg" << endl;
77
        cbg = parentWidget()->palette().color(QPalette::Window);
78
        //cbg = QColor(Qt::magenta);
79
    }
80

  
81
    deepdbg() << "paintEvent(): cell background=" << cbg.name() << endl;
82

  
83
    if ( cell.fg.isValid() ) {
84
        deepdbg() << "paintEvent(): valid cell fg" << endl;
85
        cfg = QColor(cell.fg.rgb());
86
        //cfg = QColor(Qt::blue);
87
    } else {
88
        deepdbg() << "paintEvent(): invalid cell fg" << endl;
89
        cfg = parentWidget()->palette().color(QPalette::WindowText);
90
        //cfg = QColor(Qt::cyan);
91
    }
92
    deepdbg() << "paintEvent(): cell foreground=" << cfg.name() << endl;
93

  
94

  
58 95
    QPainter p( this );
96
    QRect r = rect();
97
	YViewIface::CursorShape s = shape();
98
    deepdbg() << "paintEvent(): shape=" << s << endl;
99
    switch ( s ) {
100
		case YViewIface::CursorFilledRect :
101
        p.setPen( cbg );
102
        p.setBackground( cfg );
103
        // erase with cell foreground
104
        p.eraseRect( rect() );
105
        // paint character with cell background
106
        p.drawText( rect(), cell.c );
107
        break;
59 108

  
60
#define GET_cell \
61
    KYView* yzview = static_cast<KYView*>( parentWidget()->parentWidget() ); \
62
    YDrawCell cell = yzview->getCursorDrawCell( )
63
#define SET_pen \
64
    p.setPen( cell.bg.isValid() ? QColor( cell.bg.rgb() ) : parentWidget()->palette().window().color() );
109
		case YViewIface::CursorFrameRect :
110
        p.setPen( cfg );
111
        p.setBackground( cbg );
112
        // erase with cell background
113
        p.eraseRect( r );
114
        // paint character with cell foreground
115
        p.drawText( r, cell.c );
65 116

  
66
    switch ( type() ) {
67
    case SQUARE : {
68
            GET_cell;
69
            SET_pen;
70
            p.setBackground( cell.fg.isValid() ? QBrush( cell.fg.rgb() ) : parentWidget()->palette().windowText() );
71
            p.eraseRect( rect() );
72
            p.drawText( rect(), cell.c );
73
        }
117
        // paint rect with cell foreground
118
        r.adjust(0, 0, -1, -1);
119
        p.drawRect( r );
74 120
        break;
75
    case RECT : {
76
            GET_cell;
77
            SET_pen;
78
            p.drawRect( rect() );
79
        }
121

  
122
		case YViewIface::CursorVbar :
123
        r.setWidth( 2 );
124
        p.fillRect( r, QBrush(cfg) );
80 125
        break;
81
    case VBAR :
82
        p.fillRect( rect(), parentWidget()->palette().text() );
126

  
127
		case YViewIface::CursorHbar:
128
        // erase with cell background
129
        p.eraseRect( r );
130
        // paint character with cell foreground
131
        p.drawText( r, cell.c );
132
        r.setTop( r.bottom() - 2 );
133
        p.fillRect( r, QBrush(cfg) );
83 134
        break;
84
    case HBAR :
85
        p.fillRect( rect(), parentWidget()->palette().text() );
86
        break;
135

  
136
		case YViewIface::CursorHidden:
137
        // erase with cell background
138
        p.eraseRect( r );
139
        // paint character with cell foreground
140
        p.drawText( r, cell.c );
141
        return ;
87 142
    }
88 143
}
89 144

  
b/kpart_yzis/kycursor.h Fri Jun 27 16:47:07 2008 -0500
21 21
#ifndef _KY_CURSOR_H_
22 22
#define _KY_CURSOR_H_
23 23

  
24
#include "viewiface.h"
25

  
24 26
#include <QPixmap>
25 27
#include <QWidget>
26 28

  
......
30 32
{
31 33
    Q_OBJECT
32 34
public :
33
    enum shape {
34
        SQUARE,
35
        VBAR,
36
        HBAR,
37
        RECT,
38
    };
39

  
40
    KYCursor( KYEditor* parent, shape type );
35
    KYCursor( KYEditor* editor, YViewIface::CursorShape shape );
41 36
    virtual ~KYCursor();
42 37

  
43
    void setCursorType( shape type );
44
    shape type() const;
38
    void setCursorShape( YViewIface::CursorShape shape );
39
    YViewIface::CursorShape shape() const;
45 40

  
46 41
protected :
47 42
    virtual void paintEvent( QPaintEvent* event );
48 43

  
49 44
private :
50
    shape mCursorType;
45
    YViewIface::CursorShape mCursorShape;
46
    KYEditor *mEditor;
51 47
};
52 48

  
53 49
#endif
b/kpart_yzis/kyeditor.cpp Fri Jun 27 16:47:07 2008 -0500
42 42
#include <libyzis/buffer.h>
43 43
#include <libyzis/action.h>
44 44

  
45
KYEditor::KYEditor(KYView* parent)
46
        : QWidget(parent)
45
KYEditor::KYEditor(KYView* view)
46
        : QWidget(view)
47 47
{
48
    mParent = parent;
48
    mView = view;
49 49
    setFocusPolicy( Qt::StrongFocus );
50 50
    mUseArea.setCoords( 0, 0, 0, 0 );
51 51

  
......
61 61
    // for Input Method
62 62
    setAttribute( Qt::WA_InputMethodEnabled, true );
63 63

  
64
    mCursor = new KYCursor( this, KYCursor::SQUARE );
64
    mCursor = new KYCursor( this, YViewIface::CursorFilledRect );
65 65

  
66 66
    QTimer::singleShot(0, static_cast<KYSession*>(YSession::self()), SLOT(frontendGuiReady()) );
67 67
}
......
76 76
    p.setColor( QPalette::Window, bg );
77 77
    QWidget::setPalette( p );
78 78
    setWindowOpacity( opacity );
79
}
80

  
81
KYCursor::shape KYEditor::cursorShape()
82
{
83
    KYCursor::shape s;
84

  
85
    QString shape;
86
    YMode::ModeType m = mParent->modePool()->current()->modeType();
87
    switch ( m ) {
88
    case YMode::ModeInsert :
89
        shape = mParent->getLocalStringOption("cursorinsert");
90
        break;
91
    case YMode::ModeReplace :
92
        shape = mParent->getLocalStringOption("cursorreplace");
93
        break;
94
    case YMode::ModeCompletion :
95
        shape = "keep";
96
        break;
97
    default :
98
        shape = mParent->getLocalStringOption("cursor");
99
        break;
100
    }
101
    if ( shape == "hbar" ) {
102
        s = KYCursor::HBAR;
103
    } else if ( shape == "vbar" ) {
104
        s = KYCursor::VBAR;
105
    } else if ( shape == "keep" ) {
106
        s = mCursor->type();
107
    } else {
108
        if ( hasFocus() )
109
            s = KYCursor::SQUARE;
110
        else
111
            s = KYCursor::RECT;
112
    }
113
    return s;
114 79
}
115 80

  
116 81
QPoint KYEditor::translatePositionToReal( const YCursor& c ) const
......
136 101

  
137 102
YCursor KYEditor::translateRealToAbsolutePosition( const QPoint& p, bool ceil ) const
138 103
{
139
    return translateRealToPosition( p, ceil ) + mParent->getScreenPosition();
140
}
141

  
142
void KYEditor::updateCursor()
143
{
144
    mCursor->setCursorType( cursorShape() );
145
    mCursor->update();
104
    return translateRealToPosition( p, ceil ) + mView->getScreenPosition();
146 105
}
147 106

  
148 107

  
149 108
void KYEditor::updateArea( )
150 109
{
151
    updateCursor();
110
    mCursor->update();
152 111

  
153 112
    int lines = height() / fontMetrics().lineSpacing();
154 113
    int columns = width() / fontMetrics().maxWidth();
155 114
    mUseArea.setBottomRight( QPoint( columns * fontMetrics().maxWidth(), lines * fontMetrics().lineSpacing()) );
156
    mParent->setVisibleArea( columns, lines );
115
    mView->setVisibleArea( columns, lines );
157 116
}
158 117

  
159 118
/**
......
183 142
        modifiers |= YKey::Mod_Ctrl;
184 143

  
185 144
    YKey key( YKey::Key_Invalid, modifiers );
186
    if ( !mParent->containsKey( e->key() ) ) {
145
    if ( !mView->containsKey( e->key() ) ) {
187 146
        if ( e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z && modifiers & YKey::Mod_Ctrl )
188 147
             key.setKey( QChar(e->key()).toLower() );
189 148
        else
190 149
             key.setKey( e->text()[0] );
191 150
    } else {
192
        key.setKey( mParent->getKey( e->key() ) );
151
        key.setKey( mView->getKey( e->key() ) );
193 152
    }
194 153

  
195
    KYSession::self()->sendKey(mParent, key );
154
    KYSession::self()->sendKey(mView, key );
196 155

  
197 156
    e->accept();
198 157
}
......
202 161
    /*
203 162
    FIXME: How to handle mouse events commented out now so kyzis will compile
204 163

  
205
    if ( mParent->myBuffer()->introShown() ) {
206
     mParent->myBuffer()->clearIntro();
207
     mParent->gotodxdy( 0, 0 );
164
    if ( mView->myBuffer()->introShown() ) {
165
     mView->myBuffer()->clearIntro();
166
     mView->gotodxdy( 0, 0 );
208 167
     return;
209 168
    }
210 169
    */
......
212 171
    // leave visual mode if the user clicks somewhere
213 172
    // TODO: this should only be done if the left button is used. Right button
214 173
    // should extend visual selection, like in vim.
215
    if ( mParent->modePool()->current()->isSelMode() )
216
        mParent->modePool()->pop();
174
    if ( mView->modePool()->current()->isSelMode() )
175
        mView->modePool()->pop();
217 176

  
218 177
    if (( e->button() == Qt::LeftButton ) || ( e->button() == Qt::RightButton )) {
219
        if (mParent->modePool()->currentType() != YMode::ModeEx) {
220
            mParent->gotodxdy( e->x() / ( fontMetrics().maxWidth() ) + mParent->getDrawCurrentLeft( ),
221
                                e->y() / fontMetrics().lineSpacing() + mParent->getDrawCurrentTop( ) );
222
            mParent->updateStickyCol();
178
        if (mView->modePool()->currentType() != YMode::ModeEx) {
179
            mView->gotodxdy( e->x() / ( fontMetrics().maxWidth() ) + mView->getDrawCurrentLeft( ),
180
                                e->y() / fontMetrics().lineSpacing() + mView->getDrawCurrentTop( ) );
181
            mView->updateStickyCol();
223 182
        }
224 183
    } else if ( e->button() == Qt::MidButton ) {
225 184
        QString text = KApplication::clipboard()->text( QClipboard::Selection );
......
227 186
        if ( text.isNull() )
228 187
            text = QApplication::clipboard()->text( QClipboard::Clipboard );
229 188
        if ( ! text.isNull() ) {
230
            if ( mParent->modePool()->current()->isEditMode() ) {
189
            if ( mView->modePool()->current()->isEditMode() ) {
231 190
                QChar reg = '\"';
232 191
                KYSession::self()->setRegister( reg, text.split( "\n" ) );
233
                mParent->myBuffer()->action()->pasteContent( mParent, reg, false );
234
                mParent->moveRight();
192
                mView->myBuffer()->action()->pasteContent( mView, reg, false );
193
                mView->moveRight();
235 194
            }
236 195
        }
237 196
    }
......
240 199
void KYEditor::mouseMoveEvent( QMouseEvent *e )
241 200
{
242 201
    if (e->button() == Qt::LeftButton) {
243
        if (mParent->modePool()->currentType() == YMode::ModeCommand) {
202
        if (mView->modePool()->currentType() == YMode::ModeCommand) {
244 203
            // start visual mode when user makes a selection with the left mouse button
245
            mParent->modePool()->push( YMode::ModeVisual );
246
        } else if (mParent->modePool()->current()->isSelMode() ) {
204
            mView->modePool()->push( YMode::ModeVisual );
205
        } else if (mView->modePool()->current()->isSelMode() ) {
247 206
            // already in visual mode - move cursor if the mouse pointer has moved over a new char
248 207
            int newX = e->x() / fontMetrics().maxWidth()
249
                       + mParent->getDrawCurrentLeft();
208
                       + mView->getDrawCurrentLeft();
250 209
            int newY = e->y() / fontMetrics().lineSpacing()
251
                       + mParent->getDrawCurrentTop();
210
                       + mView->getDrawCurrentTop();
252 211

  
253
            if (newX != mParent->getCursor().x() || newY != mParent->getCursor().y()) {
254
                mParent->gotodxdy( newX, newY );
212
            if (newX != mView->getCursor().x() || newY != mView->getCursor().y()) {
213
                mView->gotodxdy( newX, newY );
255 214
            }
256 215
        }
257 216
    }
......
259 218

  
260 219
void KYEditor::focusInEvent ( QFocusEvent * )
261 220
{
262
    KYSession::self()->setCurrentView( mParent );
263
    updateCursor();
221
    KYSession::self()->setCurrentView( mView );
222
    mCursor->update();
264 223
}
265 224
void KYEditor::focusOutEvent ( QFocusEvent * )
266 225
{
267
    updateCursor();
226
    mCursor->update();
268 227
}
269 228

  
270 229

  
......
276 235

  
277 236
void KYEditor::paintEvent( QPaintEvent* pe )
278 237
{
279
    updateCursor();
238
    mCursor->update();
280 239

  
281 240
    // convert QPaintEvent rect to yzis coordinate
282 241
    QRect r = pe->rect();
......
285 244
    //dbg() << "QYisEdit::paintEvent : " << pe->rect().topLeft() << "," << pe->rect().bottomRight() <<
286 245
    //                              " => " << r.topLeft() << "," << r.bottomRight() << endl;
287 246
    // paint it
288
    mParent->guiPaintEvent( mParent->clipSelection( YSelection( r ) ) );
247
    mView->guiPaintEvent( mView->clipSelection( YSelection( r ) ) );
289 248
}
290 249

  
291 250
void KYEditor::setCursor( int c, int l )
292 251
{
293 252
    // yzDebug() << "setCursor" << endl;
294
    c = c - mParent->getDrawCurrentLeft();
295
    l -= mParent->getDrawCurrentTop();
253
    c = c - mView->getDrawCurrentLeft();
254
    l -= mView->getDrawCurrentTop();
296 255
    unsigned int x = c * fontMetrics().maxWidth();
297
    if ( mParent->getLocalBooleanOption( "rightleft" ) ) {
256
    if ( mView->getLocalBooleanOption( "rightleft" ) ) {
298 257
        x = width() - x - mCursor->width();
299 258
    }
300 259
    mCursor->move( x, l * fontMetrics().lineSpacing() );
......
303 262

  
304 263
    // need for InputMethod (OverTheSpot)
305 264
    // setMicroFocusHint( mCursor->x(), mCursor->y(), mCursor->width(), mCursor->height() );
265
}
266

  
267
void KYEditor::setCursorShape( YViewIface::CursorShape cs )
268
{
269
	mCursor->setCursorShape(cs);
306 270
}
307 271

  
308 272
QPoint KYEditor::cursorCoordinates( )
......
320 284
    cursorRect.moveTo( mCursor->pos() );
321 285
    update( cursorRect );
322 286
    QWidget::scroll( rx, ry, mUseArea );
287
}
288

  
289
KYView * KYEditor::view() const
290
{
291
	return mView;
323 292
}
324 293

  
325 294
void KYEditor::guiDrawCell( QPoint pos, const YDrawCell& cell, QPainter* p )
......
345 314
        r.setTopLeft( translatePositionToReal( YCursor(pos) ) );
346 315
        r.setRight( width() );
347 316
        r.setHeight( fontMetrics().lineSpacing() );
348
        int nb_char = mParent->getColumnsVisible() - pos.x();
317
        int nb_char = mView->getColumnsVisible() - pos.x();
349 318
        p->drawText( r, QString( nb_char, clearChar ) );
350 319
    }
351 320
}
......
370 339
/*
371 340
void KYEditor::imStartEvent( QIMEvent *e )
372 341
{
373
 if ( mParent->mParent->modePool()->current()->supportsInputMethod() ) {
374
  mParent->mParent->modePool()->current()->imBegin( mParent );
342
 if ( mView->mView->modePool()->current()->supportsInputMethod() ) {
343
  mView->mView->modePool()->current()->imBegin( mView );
375 344
 }
376 345
 e->accept();
377 346
}*/
......
380 349
/*
381 350
void KYEditor::imComposeEvent( QIMEvent *e ) {
382 351
 //yzDebug() << "KYEditor::imComposeEvent text=" << e->text() << " len=" << e->selectionLength() << " pos=" << e->cursorPos() << endl;
383
 if ( mParent->mParent->modePool()->current()->supportsInputMethod() ) {
384
  mParent->mParent->modePool()->current()->imCompose( mParent, e->text() );
352
 if ( mView->mView->modePool()->current()->supportsInputMethod() ) {
353
  mView->mView->modePool()->current()->imCompose( mView, e->text() );
385 354
  e->accept();
386 355
 } else {
387 356
  e->ignore();
......
392 361
/*
393 362
void KYEditor::imEndEvent( QIMEvent *e ) {
394 363
// yzDebug() << "KYEditor::imEndEvent text=" << e->text() << " len=" << e->selectionLength() << " pos=" << e->cursorPos() << endl;
395
 if ( mParent->mParent->modePool()->current()->supportsInputMethod() ) {
396
  mParent->mParent->modePool()->current()->imEnd( mParent, e->text() );
364
 if ( mView->mView->modePool()->current()->supportsInputMethod() ) {
365
  mView->mView->modePool()->current()->imEnd( mView, e->text() );
397 366
 } else {
398
  mParent->sendKey( e->text() );
367
  mView->sendKey( e->text() );
399 368
 }
400 369
 e->accept();
401 370
}*/
b/kpart_yzis/kyeditor.h Fri Jun 27 16:47:07 2008 -0500
43 43
    //append text
44 44
    void append ( const QString& );
45 45

  
46
    /**
47
     * move the cursor to the specificed column and line
48
     *
49
     * @param c column number
50
     * @param l line number
51
     */
46 52
    void setCursor(int c, int l);
53

  
54
    /**
55
     * set the cursor shape
56
     *
57
     * @param cs the new cursor shape
58
     */
59
    void setCursorShape( YViewIface::CursorShape cs );
60

  
47 61
    void scroll(int x, int y);
48 62

  
63
    /**
64
     * @return the view associated with this editor
65
     */
66
    KYView * view() const;
49 67

  
50
    KYCursor::shape cursorShape();
51
    void updateCursor();
52 68
    // update text area
53 69
    void updateArea( );
54 70

  
......
113 129
private :
114 130

  
115 131
    KYCursor* mCursor;
116
    KYView* mParent;
132
    KYView* mView;
117 133
    QRect mUseArea;
118 134
};
119 135

  
b/kpart_yzis/kyview.cpp Fri Jun 27 16:47:07 2008 -0500
116 116
    m_editor->setCursor( viewCursor().screenX(), viewCursor().screenY() );
117 117
}
118 118

  
119
void KYView::guiCursorChanged( YViewIface::CursorShape cs )
120
{
121
	m_editor->setCursorShape( cs );
122
}
123

  
119 124
void KYView::guiUpdateMode()
120 125
{
121
    m_editor->updateCursor();
122 126
}
123 127

  
124 128
bool KYView::guiPopupFileSaveAs()
b/kpart_yzis/kyview.h Fri Jun 27 16:47:07 2008 -0500
54 54
    virtual YStatusBarIface* guiStatusBar();
55 55
    virtual void guiUpdateFileName();
56 56
    virtual void guiUpdateCursor();
57
    virtual void guiCursorChanged( YViewIface::CursorShape cs );
57 58
    virtual void guiUpdateMode();
58 59
    virtual void guiHighlightingChanged();
59 60
    virtual void guiNotifyContentChanged(const YSelection&);
b/qyzis/qyedit.cpp Fri Jun 27 16:47:07 2008 -0500
115 115
	mCursor->setCursorShape( cs );
116 116
}
117 117

  
118
void QYEdit::updateCursor()
118
void QYEdit::modeChanged()
119 119
{
120 120
    mCursor->update();
121
}
122

  
123
void QYEdit::modeChanged()
124
{
125
    updateCursor();
126 121
}
127 122

  
128 123
void QYEdit::updateArea( )
129 124
{
130 125
    dbg() << "updatearea()" << endl;
131
    updateCursor();
126
    mCursor->update();
132 127

  
133 128
    dbg() << "updateArea(): fixedPitch = " << fontInfo().fixedPitch() << endl;
134 129
    dbg() << "updateArea(): lineheight = " << fontMetrics().lineSpacing() << endl;
......
256 251
    dbg() << "focusInEvent() for " << mView->myBuffer()->fileNameShort() << endl;
257 252
	mView->setCursorShape( mPreviousCursorShape );
258 253
    YSession::self()->setCurrentView( mView );
259
    updateCursor();
254
    mCursor->update();
260 255
}
261 256
void QYEdit::focusOutEvent ( QFocusEvent * )
262 257
{
......
265 260
	if(mPreviousCursorShape != YViewIface::CursorHidden) {
266 261
		mView->setCursorShape( YViewIface::CursorFrameRect );
267 262
	}
268
    updateCursor();
263
    mCursor->update();
269 264
}
270 265

  
271 266
void QYEdit::resizeEvent(QResizeEvent* e)
......
276 271

  
277 272
void QYEdit::paintEvent( QPaintEvent* pe )
278 273
{
279
    updateCursor();
274
    mCursor->update();
280 275
    // convert QPaintEvent rect to yzis coordinate
281 276
    QRect r = pe->rect();
282 277
    r.setTopLeft( translateRealToAbsolutePosition( r.topLeft() ) );