2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
12 #include <QApplication>
14 #include <QPaintEvent>
15 #include <QMouseEvent>
18 #include <QHBoxLayout>
19 #include <QVBoxLayout>
20 #include <QScrollArea>
21 #include <QToolButton>
26 #include <kstandarddirs.h>
27 #include "mastersettings.h"
28 #include "pref_theme.h"
29 #include "movelist_widget.h"
30 #include "movelist_table.h"
31 #include "movelist_notifier.h"
32 #include "movelist_p.h"
37 #define MARGIN_RIGHT 4
39 #define MARGIN_BOTTOM 0
41 #define COMMENT_INDENTATION 4
42 #define VAR_INDENTATION 16
44 #define BORDER_RIGHT 3
46 #define BORDER_BOTTOM 3
47 #define MIN_COL_WIDTH 5.0
49 #define DEFAULT_ANIMATION_TIME 350.0
51 //BEGIN FancyItem--------------------------------------------------------------
53 bool FancyItem::showing() {
54 return !(time_opacity
!=-1 && target_opacity
== 0) &&
55 ((time_opacity
!=-1 && target_opacity
== 255) || (visible() && opacity() == 255));
58 void FancyItem::appear() {
59 if((time_opacity
!=-1 && target_opacity
== 255)
60 || (visible() && opacity() == 255))
63 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
64 if(!m
->m_settings
->anim_enabled
|| !m
->m_settings
->anim_hideshow
)
69 time_opacity
= m
->layout_time
;
74 void FancyItem::disappear() {
75 if((time_opacity
!=-1 && target_opacity
== 0)
76 || !visible() || opacity() == 0)
79 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
81 if(!m
->m_settings
->anim_enabled
|| !m
->m_settings
->anim_hideshow
)
86 time_opacity
= m
->layout_time
;
91 void FancyItem::goTo(QPoint p
) {
92 if((time_pos
!=-1 && target_pos
== p
) || (time_pos
==-1 && pos() == p
))
95 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
96 if(!m
->m_settings
->anim_enabled
|| !m
->m_settings
->anim_moving
)
101 time_pos
= m
->layout_time
;
102 /*std::cout << m->layout_time << " start " << this << " "
103 << prettyTypeName(typeid(*this).name()) << std::endl;*/
108 void FancyItem::setHighlight(bool h
) {
112 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
113 if(!m
->m_settings
->anim_enabled
|| !m
->m_settings
->anim_highlight
) {
114 curr_highlight
= h
? 255 : 0;
119 old_highlight
= highlighted
? 255 : 0;
120 target_highlight
= h
? 255 : 0;
122 time_highlight
= m
->mSecs();
127 void FancyItem::advance(int time
) {
128 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
129 /*std::cout << time << " anim " << this << " "
130 << prettyTypeName(typeid(*this).name()) << std::endl;*/
131 if(time_highlight
!= -1) {
132 float fact
= (time
- time_highlight
) / m
->m_settings
->anim_time
;
134 curr_highlight
= target_highlight
;
138 curr_highlight
= int(target_highlight
*fact
+ old_highlight
*(1-fact
));
141 if(time_opacity
!= -1) {
142 float fact
= (time
- time_opacity
) / m
->m_settings
->anim_time
;
144 setOpacity(target_opacity
);
145 setVisible(target_opacity
!= 0);
149 setOpacity( int(target_opacity
*fact
+ old_opacity
*(1-fact
)) );
154 float fact
= (time
- time_pos
) / m
->m_settings
->anim_time
;
156 /*std::cout << time << " done " << this << " "
157 << prettyTypeName(typeid(*this).name()) << std::endl;*/
162 /*std::cout << time << " move " << this << " "
163 << prettyTypeName(typeid(*this).name()) << std::endl;*/
164 moveTo( int(target_pos
.x()*fact
+ old_pos
.x()*(1-fact
)+0.5),
165 int(target_pos
.y()*fact
+ old_pos
.y()*(1-fact
)+0.5));
169 /*std::cout << time << " stop " << this << " "
170 << prettyTypeName(typeid(*this).name()) << std::endl;*/
175 bool FancyItem::layered() const {
179 //END FancyItem----------------------------------------------------------------
182 //BEGIN Brace------------------------------------------------------------------
184 void Brace::setHeight(int h
) {
186 if((animated() && target_height
== h
) || height
== h
)
189 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
190 if(!m
->m_settings
->anim_enabled
|| !m
->m_settings
->anim_moving
) {
197 time_height
= m
->layout_time
;
203 void Brace::advance(int time
) {
204 if(time_height
!= -1) {
205 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
206 float fact
= (time
- time_height
) / m
->m_settings
->anim_time
;
208 height
= target_height
;
212 height
= int(target_height
*fact
+ old_height
*(1-fact
) + 0.5);
215 FancyItem::advance(time
);
218 void Brace::paint (QPainter
*p
) {
219 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
220 if(height
< m
->entry_size
)
222 QPointF
p1((pos().x()*2+width
)/2.0, (pos().y()*2+m
->entry_size
)/2.0);
223 QPointF
p2((pos().x()*2+width
)/2.0, (pos().y()*2+height
*2-m
->entry_size
)/2.0);
227 p
->setBrush(QColor(255,192,224));
228 q
= QColor(128,0,64);
231 p
->setBrush(QColor(192,255,224));
232 q
= QColor(0,128,64);
236 p
->setRenderHint(QPainter::Antialiasing
);
237 int s
= std::min(m
->entry_size
, width
);
238 float cs1
= (0.4 + 0.2*(curr_highlight
/255.0)) * s
;
242 p
->drawEllipse(QRectF(-cs1
/2,-cs1
/2,cs1
,cs1
).translated(p1
));
243 p
->setBrush(p
->pen().color());
244 p
->drawEllipse(QRectF(-cs2
/2,-cs2
/2,cs2
,cs2
).translated(p2
));
248 QRect
Brace::rect () const {
249 return QRect(pos(), QSize(width
, height
));
252 //END Brace--------------------------------------------------------------------
255 //BEGIN Text-------------------------------------------------------------------
257 void Text::paint (QPainter
*p
) {
258 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
259 if(curr_highlight
!= 0) {
260 p
->setBrush(QColor(192,224,208, curr_highlight
));
261 p
->setPen(QColor(64,128,96, curr_highlight
));
262 p
->drawRect(rect().adjusted(0,0,-1,-1));
264 p
->setFont(selected
? m
->m_settings
->sel_mv_font
: m
->m_settings
->mv_font
);
265 p
->setPen(selected
? m
->m_settings
->select_color
: Qt::black
);
266 p
->drawText(pos()+QPoint(MARGIN_LEFT
, MARGIN_TOP
+m
->m_settings
->mv_fmetrics
.ascent()), text
);
269 QRect
Text::rect () const {
270 return QRect(pos(), QSize(width
, height
));
273 void Text::doUpdate () {
277 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
278 width
= (selected
? m
->m_settings
->sel_mv_fmetrics
279 : m
->m_settings
->mv_fmetrics
).boundingRect(text
).right()
280 + MARGIN_LEFT
+ MARGIN_RIGHT
;
281 height
= m
->entry_size
;
283 needs_update
= false;
287 //END Text---------------------------------------------------------------------
290 //BEGIN Comment----------------------------------------------------------------
292 void Comment::paint (QPainter
*p
) {
293 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
295 if(curr_highlight
!= 0) {
296 p
->setBrush(QColor(255,255,255, curr_highlight
));
297 p
->setPen(QColor(192,192,192, curr_highlight
));
298 p
->drawRect(rect().adjusted(0,0,-1,-1));
300 p
->setFont(m
->m_settings
->comm_font
);
301 p
->setPen(m
->m_settings
->comment_color
);
302 p
->drawText(pos().x() + MARGIN_RIGHT
,
304 width
- MARGIN_LEFT
- MARGIN_RIGHT
, 9999,
305 Qt::AlignLeft
|Qt::AlignTop
|Qt::TextWordWrap
, text
);
308 QRect
Comment::rect () const {
309 return QRect(pos(), QSize(width
, height
));
312 void Comment::doUpdate () {
316 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
317 QPoint dest
= ((time_pos
!= -1) ? target_pos
: pos());
318 width
= std::max(m
->width() - dest
.x(), m
->entry_size
);
319 height
= m
->m_settings
->comm_fmetrics
.boundingRect(0,0,width
- MARGIN_LEFT
- MARGIN_RIGHT
, 99999,
320 Qt::AlignLeft
|Qt::AlignTop
|Qt::TextWordWrap
, text
).height()
321 + MARGIN_TOP
+ MARGIN_BOTTOM
;
323 needs_update
= false;
327 //END Comment------------------------------------------------------------------
330 //BEGIN Entry------------------------------------------------------------------
332 void Entry::paint (QPainter
*p
) {
333 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
334 if(curr_highlight
!= 0) {
335 p
->setBrush(QColor(192,224,255, curr_highlight
));
336 p
->setPen(QColor(64,96,128, curr_highlight
));
337 p
->drawRect(rect().adjusted(0,0,-1,-1));
339 p
->setPen(selected
? m
->m_settings
->select_color
: Qt::black
);
340 int x
= pos().x()+MARGIN_LEFT
;
341 int y
= pos().y()+MARGIN_TOP
+m_ascent
;
343 p
->setRenderHint(QPainter::TextAntialiasing
);
344 QFont tf
= selected
? m
->m_settings
->sel_mv_font
: m
->m_settings
->mv_font
;
345 QFontMetrics
& fm
= selected
? m
->m_settings
->sel_mv_fmetrics
: m
->m_settings
->mv_fmetrics
;
348 for(int i
=0;i
<(int)move
.size();i
++) {
349 if(move
[i
].m_type
== MovePart::Text
) {
351 p
->drawText(QPoint(x
+r
.width(), y
), move
[i
].m_string
);
352 QRect b
= fm
.boundingRect(move
[i
].m_string
);
353 r
|= b
.translated(r
.width()-b
.x(), 0);
355 else if(move
[i
].m_type
== MovePart::Figurine
) {
356 ::Loader::Glyph g
= m
->m_loader
.getValue
< ::Loader::Glyph
>(move
[i
].m_string
);
357 QFont font
= g
.fontValid() ? g
.font() : tf
;
359 p
->drawText(QPoint(x
+r
.width(), y
), g
.str());
360 QFontMetrics
fi(font
);
361 QRect b
= fi
.boundingRect(g
.str());
362 r
|= b
.translated(r
.width()-b
.x(), 0);
367 QRect
Entry::rect () const {
368 return m_rect
.translated(pos().x()+MARGIN_LEFT
, pos().y()+MARGIN_TOP
+m_ascent
);
371 void Entry::doUpdate () {
375 Widget
*m
= dynamic_cast<Widget
*>(topLevelCanvas());
376 QFont tf
= selected
? m
->m_settings
->sel_mv_font
: m
->m_settings
->mv_font
;
377 QFontMetrics
& fm
= selected
? m
->m_settings
->sel_mv_fmetrics
: m
->m_settings
->mv_fmetrics
;
378 m_ascent
= m
->m_settings
->mv_fmetrics
.ascent();
379 m_rect
= QRect(0,0,0,0);
381 for(int i
=0;i
<(int)move
.size();i
++) {
382 if(move
[i
].m_type
== MovePart::Text
) {
383 QRect b
= fm
.boundingRect(move
[i
].m_string
);
384 m_rect
|= b
.translated(m_rect
.width()-b
.x(), 0);
386 else if(move
[i
].m_type
== MovePart::Figurine
) {
387 ::Loader::Glyph g
= m
->m_loader
.getValue
< ::Loader::Glyph
>(move
[i
].m_string
);
388 QFontMetrics
fi(g
.fontValid() ? g
.font() : tf
);
389 QRect b
= fi
.boundingRect(g
.str());
390 m_rect
|= b
.translated(m_rect
.width()-b
.x(), 0);
393 m_rect
= QRect(m_rect
.x(),m_rect
.y(),m_rect
.width()+MARGIN_RIGHT
,m_rect
.height());
395 needs_update
= false;
399 //END Entry--------------------------------------------------------------------
402 //BEGIN Settings---------------------------------------------------------------
404 void Settings::load() {
405 ::Settings s
= settings().group("move-list");
406 ::Settings s_anim
= s
.group("animations");
408 anim_enabled
= s
.group("animations").flag("enabled", true);
409 anim_moving
= s_anim
.group("moving").flag("enabled", true);
410 anim_hideshow
= s_anim
.group("hideshow").flag("enabled", true);
411 anim_highlight
= s_anim
.group("highlight").flag("enabled", true);
412 anim_speed
= s_anim
["speed"] | 16;
413 anim_time
= DEFAULT_ANIMATION_TIME
*pow(5.0, 1.0 - anim_speed
/16.0);
414 anim_smoothness
= s_anim
["smoothness"] | 16;
415 select_color
= s
["select-color"] | QColor(Qt::red
);
416 comment_color
= s
["comment-color"] | QColor(64,64,64);
417 mv_font
= QApplication::font();
418 if ((use_mv_font
= s
.group("moves-font").flag("enabled", true)))
419 mv_font
= s
["moves-font"] | mv_font
;
420 sel_mv_font
= mv_font
;
421 sel_mv_font
.setBold(true);
422 comm_font
= QApplication::font();
423 comm_font
.setItalic(true);
424 if ((use_comm_font
= s
.group("comment-font").flag("enabled", true)))
425 comm_font
= s
["CommentFont"] | comm_font
;
426 mv_fmetrics
= QFontMetrics(mv_font
);
427 sel_mv_fmetrics
= QFontMetrics(sel_mv_font
);
428 comm_fmetrics
= QFontMetrics(comm_font
);
431 void Settings::save() {
432 ::Settings s
= settings().group("move-list");
433 ::Settings s_anim
= s
.group("animations");
435 s
.group("animations").setFlag("enabled", anim_enabled
);
436 s_anim
.group("moving").setFlag("enabled", anim_moving
);
437 s_anim
.group("hideshow").setFlag("enabled", anim_hideshow
);
438 s_anim
.group("highlight").setFlag("enabled", anim_highlight
);
439 s_anim
["speed"] = anim_speed
;
440 s_anim
["smoothness"] = anim_smoothness
;
441 s
["select-color"] = select_color
;
442 s
["comment-color"] = comment_color
;
443 s
.group("moves-font").flag("enabled", use_mv_font
);
444 s
["moves-font"] = mv_font
;
445 s
.group("comment-font").flag("enabled", use_comm_font
);
446 s
["comment-font"] = comm_font
;
449 //END Settings-----------------------------------------------------------------
452 //BEGIN Widget-----------------------------------------------------------------
454 Widget::Widget(QWidget
*parent
, Table
*o
)
455 : KGameCanvasWidget(parent
)
458 , comment_editor(NULL
)
459 , layout_pending(false)
461 , layout_goto_selected(false)
462 , layout_width_changed(true)
463 , layout_must_relayout(true)
466 , m_settings(new Settings
) {
469 setSizePolicy ( QSizePolicy::MinimumExpanding
, QSizePolicy::Minimum
);
471 setMouseTracking(true);
480 void Widget::reset() {
481 curr_highlight
= Index(-1);
482 curr_selected
= Index(-1);
484 delete comment_editor
;
485 comment_editor
= NULL
;
489 mv
.push_back(MovePart(QString("Mainline:")));
491 history
.push_back( EntryPtr(new Entry(-1, mv
, Index(0), this)) );
496 EntryPtr
Widget::fetch(const Index
& ix
) {
498 History
*vec
= fetchRef(ix
, &at
);
499 return vec
? (*vec
)[at
] : EntryPtr();
502 History
* Widget::fetchRef(const Index
& ix
, int* idx
) {
503 if(ix
.num_moves
>= (int)history
.size() || ix
.num_moves
< 0 )
506 History
* aretv
= &history
;
507 EntryPtr retv
= history
[ix
.num_moves
];
508 if(idx
) *idx
= ix
.num_moves
;
510 for(int i
=0; i
<(int)ix
.nested
.size();i
++) {
511 Variations::iterator it
= retv
->variations
.find(ix
.nested
[i
].variation
);
512 if(it
== retv
->variations
.end() || ix
.nested
[i
].num_moves
>= (int)it
->second
.size()
513 || ix
.nested
[i
].num_moves
< 0 )
517 retv
= it
->second
[ix
.nested
[i
].num_moves
];
518 if(idx
) *idx
= ix
.nested
[i
].num_moves
;
523 Notifier
* Widget::getNotifier() {
527 void Widget::setNotifier(Notifier
* n
, bool detach_prev
){
528 if(detach_prev
&& notifier
&& notifier
!= n
)
529 notifier
->onDetachNotifier();
533 void Widget::settingsChanged() {
536 setAnimationDelay( int(70.0*pow(10.0, -m_settings
->anim_smoothness
/32.0)) );
538 entry_size
= m_settings
->mv_fmetrics
.height()+MARGIN_TOP
+MARGIN_BOTTOM
;
539 owner_table
->m_scroll_area
->setMinimumSize(entry_size
*6, entry_size
*9);
541 m_loader
.setSize(m_settings
->mv_font
.pointSize());
543 layout_must_relayout
= true;
547 void Widget::mouseMoveEvent ( QMouseEvent
* event
) {
548 KGameCanvasItem
*i
= itemAt(event
->pos());
549 Entry
* e
= i
? dynamic_cast<Entry
*>(i
) : NULL
;
550 Text
* f
= i
? dynamic_cast<Text
*>(i
) : NULL
;
551 Brace
* b
= i
? dynamic_cast<Brace
*>(i
) : NULL
;
552 Comment
* c
= i
? dynamic_cast<Comment
*>(i
) : NULL
;
553 EntryPtr olde
= fetch(curr_highlight
);
554 f
= f
&& f
->type
== 1 ? f
: NULL
;
556 int oldtype
= curr_highlight_type
;
559 if(curr_highlight
== e
->index
&& curr_highlight_type
==-1)
561 e
->setHighlight(true);
562 curr_highlight
= e
->index
;
563 curr_highlight_type
= -1;
566 if(curr_highlight
== f
->entry
->index
&& curr_highlight_type
==-2)
568 f
->setHighlight(true);
569 curr_highlight
= f
->entry
->index
;
570 curr_highlight_type
= -2;
573 if( (curr_highlight
==c
->entry
->index
) &&
574 ((c
->variation
==-1) ? (curr_highlight_type
== -3) :
575 (curr_highlight_type
== -1000-c
->variation
) ) )
577 c
->setHighlight(true);
578 curr_highlight
= c
->entry
->index
;
579 curr_highlight_type
= (c
->variation
==-1) ? -3 : (-1000-c
->variation
);
582 if(curr_highlight
== b
->entry
->index
&& curr_highlight_type
==b
->variation
)
584 b
->setHighlight(true);
585 curr_highlight
= b
->entry
->index
;
586 curr_highlight_type
= b
->variation
;
589 curr_highlight
= Index(-1);
593 olde
->setHighlight(false);
594 else if(oldtype
==-2 && olde
->fregna
)
595 olde
->fregna
->setHighlight(false);
596 else if(oldtype
==-3 && olde
->comment
)
597 olde
->comment
->setHighlight(false);
598 else if(oldtype
<=-1000 && olde
->vcomments
.count(-1000-oldtype
)==1)
599 olde
->vcomments
[-1000-oldtype
]->setHighlight(false);
600 else if(oldtype
>=0 && olde
->braces
.count(oldtype
)==1)
601 olde
->braces
[oldtype
]->setHighlight(false);
605 void Widget::startEditing(const Index
& i
, int v
) {
608 EntryPtr e
= fetch(i
);
610 ERROR("Invalid index " << i
);
614 CommentPtr c
= v
== -1 ? e
->comment
: (e
->vcomments
.count(v
) ?
615 e
->vcomments
[v
] : CommentPtr());
620 TextPtr n
= e
->number
;
621 if(!n
&& i
> Index(0))
622 n
= fetch(i
.prev())->number
;
623 int x
= (n
? n
->pos().x() : e
->pos().x()) + ((v
== -1) ? 0 : VAR_INDENTATION
);
625 rect
= QRect(x
, e
->pos().y()+entry_size
, width()-x
, 0);
628 edited_comment_variation
= v
;
629 edited_comment
= boost::weak_ptr
<Entry
>(e
);
630 comment_editor
= new QTextEdit(c
? c
->text
: QString(), this);
631 comment_editor
->setGeometry(rect
.adjusted(0,0,0,entry_size
*3));
632 comment_editor
->show();
633 comment_editor
->setFocus(Qt::MouseFocusReason
);
634 comment_editor
->installEventFilter(this);
637 bool Widget::eventFilter(QObject
*obj
, QEvent
*event
) {
638 if(obj
== comment_editor
&& event
->type() == QEvent::FocusOut
) {
645 void Widget::stopEditing() {
646 EntryPtr e
= edited_comment
.lock();
648 if(comment_editor
&& notifier
) {
649 QString c
= comment_editor
->toPlainText();
650 c
.replace(QRegExp("(?:[ \t]\r?\n\r?|\r?\n\r?[ \t]|\r?\n\r?)"), " ");
651 if(edited_comment_variation
== -1)
652 notifier
->onUserSetComment(e
->index
, c
);
654 notifier
->onUserSetVComment(e
->index
, edited_comment_variation
, c
);
656 edited_comment
.reset();
659 comment_editor
->deleteLater();
660 comment_editor
= NULL
;
664 void Widget::mousePressEvent ( QMouseEvent
* event
) {
667 KGameCanvasItem
*i
= itemAt(event
->pos());
671 Text
*t
= dynamic_cast<Text
*>(i
);
672 if(t
&& t
->type
== 1) {
675 e
->hide_next
= false;
679 e
->expanded
= !e
->expanded
;
684 Brace
*b
= dynamic_cast<Brace
*>(i
);
686 if(event
->button() == Qt::LeftButton
) {
688 EntryPtr first
= e
->variations
[b
->variation
][0];
689 first
->hide_next
= !first
->hide_next
;
692 else if(event
->button() == Qt::RightButton
) {
695 a
= m
.addAction(KIcon("pen"), "&Set comment");
696 a
->setData("comment");
698 a
= m
.addAction(KIcon(), "&Promote variation");
699 a
->setData("promote");
700 a
= m
.addAction(KIcon("edit-delete"), "&Remove variation");
701 a
->setData("remove");
702 boost::weak_ptr
<Entry
> ewptr
= boost::weak_ptr
<Entry
>(fetch(b
->entry
->index
));
703 int v
= b
->variation
;
705 a
= m
.exec(event
->globalPos());
707 /* beware, here, after exec, e could be a dangling pointer */
708 EntryPtr eptr
= ewptr
.lock();
709 if(a
&& notifier
&& eptr
&& eptr
->variations
.count(v
)) {
710 if(a
->data() == "comment")
711 startEditing(eptr
->index
, v
);
712 else if(a
->data() == "promote")
713 notifier
->onUserPromoteVariation(eptr
->index
.next(v
));
714 else if(a
->data() == "remove")
715 notifier
->onUserRemoveVariation(eptr
->index
.next(v
));
721 Comment
*c
= dynamic_cast<Comment
*>(i
);
723 startEditing(c
->entry
->index
, c
->variation
);
727 Entry
*e
= dynamic_cast<Entry
*>(i
);
729 if(event
->button() == Qt::LeftButton
) {
731 notifier
->onUserSelectMove(e
->index
);
733 else if(event
->button() == Qt::RightButton
) {
736 a
= m
.addAction(KIcon("pen"), "&Set comment");
737 a
->setData("comment");
738 a
= m
.addAction(KIcon("eraser"), "&Clear variations");
739 a
->setEnabled(!e
->variations
.empty());
741 a
= m
.addAction(KIcon("cut"), "&Truncate");
742 a
->setEnabled(fetch(e
->index
.next()));
743 a
->setData("truncate");
745 a
= m
.addAction(KIcon(), "&Promote variation");
746 a
->setEnabled(e
->index
.nested
.size());
747 a
->setData("promote");
748 a
= m
.addAction(KIcon("edit-delete"), "&Remove variation");
749 a
->setEnabled(e
->index
.nested
.size());
750 a
->setData("remove");
751 boost::weak_ptr
<Entry
> ewptr
= boost::weak_ptr
<Entry
>(fetch(e
->index
));
753 a
= m
.exec(event
->globalPos());
755 /* beware, here, after exec, e could be a dangling pointer */
756 EntryPtr eptr
= ewptr
.lock();
757 if(a
&& notifier
&& eptr
) {
758 if(a
->data() == "comment")
759 startEditing(eptr
->index
, -1);
760 else if(a
->data() == "clear")
761 notifier
->onUserClearVariations(eptr
->index
);
762 else if(a
->data() == "truncate")
763 notifier
->onUserTruncate(eptr
->index
);
764 else if(a
->data() == "promote")
765 notifier
->onUserPromoteVariation(eptr
->index
);
766 else if(a
->data() == "remove")
767 notifier
->onUserRemoveVariation(eptr
->index
);
774 void Widget::mouseReleaseEvent ( QMouseEvent
* /*event*/ ) {
778 void Widget::resizeEvent ( QResizeEvent
* event
) {
780 if(event
->size().width() != event
->oldSize().width()) {
781 layout_width_changed
= true;
786 void Widget::layout() {
790 layout_pending
= true;
791 QTimer::singleShot( 0, this, SLOT(doLayout()) );
794 void Widget::doLayout() {
795 layout_time
= mSecs();
796 layout_pending
= false;
797 layout_max_width
= 0;
798 //std::cout << "layout_must_relayout = " << layout_must_relayout << std::endl;
799 int h
= layoutHistory(history
, BORDER_LEFT
, BORDER_TOP
, -1, 0, 0, true);
801 QSize
s(std::max(entry_size
*7, layout_max_width
+BORDER_RIGHT
),
802 std::max(entry_size
*10, h
+BORDER_BOTTOM
) );
805 layout_width_changed
= false;
806 layout_must_relayout
= false;
807 if(layout_goto_selected
) {
808 EntryPtr e
= fetch(curr_selected
);
810 owner_table
->m_scroll_area
->ensureVisible( int(e
->pos().x() + e
->m_rect
.width()*0.5),
811 int(e
->pos().y() + e
->m_rect
.height()*0.5) );
812 layout_goto_selected
= false;
816 int Widget::layoutHistory(History
& array
, int at_x
, int at_y
,
817 int a_prev_turn
, int mv_num
, int sub_mv_num
, bool visible
) {
822 int prev_turn
= a_prev_turn
;
824 for(int i
=0;i
<(int)array
.size();i
++) {
825 EntryPtr e
= array
[i
];
827 /* if this is not visible, hide the item and hide all the number/fregna tags */
831 e
->number
->disappear();
833 e
->fregna
->disappear();
835 e
->comment
->disappear();
837 /* hide the subvariations */
838 for(Variations::iterator it
= e
->variations
.begin(); it
!= e
->variations
.end(); ++it
)
839 layoutHistory(it
->second
, 0, 0, e
->move_turn
, mv_num
, sub_mv_num
, false);
840 for(Braces::iterator it
= e
->braces
.begin(); it
!= e
->braces
.end(); ++it
)
841 it
->second
->disappear();
842 for(VComments::iterator it
= e
->vcomments
.begin(); it
!= e
->vcomments
.end(); ++it
)
843 it
->second
->disappear();
848 /* adjust the position if this is paired on the right */
849 bool draw_num
= false;
852 if(e
->move_turn
!= prev_turn
) {
859 if(layout_style
==0) {
860 if(e
->move_turn
== 0 || i
==0 || array
[i
-1]->childs_height
!= 0) {
861 if(mv_num
>=1 && (e
->move_turn
!= prev_turn
|| i
==0 || array
[i
-1]->childs_height
!= 0)) {
866 flow_x
= (mv_num
>=1) ? nflow_x
: at_x
;
869 if(e
->move_turn
!= prev_turn
) {
870 flow_x
= std::max(flow_x
+ MIDDLE_PAD
, int(MIN_COL_WIDTH
*entry_size
));
871 flow_y
-= entry_size
;
874 flow_x
= int(MIN_COL_WIDTH
*entry_size
);
878 if(e
->move_turn
!= prev_turn
|| i
==0 || array
[i
-1]->childs_height
!= 0) {
884 else if(col_num
== layout_style
) {
889 flow_y
-= entry_size
;
890 flow_x
= std::max(flow_x
+ MIDDLE_PAD
,
891 at_x
+ col_num
*int(MIN_COL_WIDTH
*entry_size
));
898 /* update the number */
900 TextPtr
& n
= e
->number
;
902 n
= TextPtr(new Text(e
.get(), 0, this));
904 n
->text
= QString::number((mv_num
+1)/2)+(mv_num
&1 ? "." : ". ...");
906 n
->text
= QString::number(mv_num
)+
907 (sub_mv_num
? "+"+QString::number(sub_mv_num
) : QString())+".";
908 n
->needs_update
= true;
910 else if( !n
->showing() || layout_must_relayout
)
911 n
->needs_update
= true;
913 /* Mh, the number should never change, only appear disappear.
914 should this change, add here the code to enable number changes. */
915 QPoint
dest(flow_x
, flow_y
);
929 e
->number
->disappear();
932 /* update the entry */
933 QPoint
dest(flow_x
, flow_y
);
939 if( !e
->showing() || layout_must_relayout
)
940 e
->needs_update
= true;
943 e
->childs_height
= 0;
944 flow_x
+= e
->m_rect
.width();
947 /* Update the fregna. The fregna is visible if there are subvariations in this
948 entry, or if this entry is the first one of a variation where the remaining
949 entries are hidden and that contains the current position */
950 bool expandable
= !e
->variations
.empty() || e
->comment
;
951 bool sel
= (e
->hide_next
&& e
->index
<curr_selected
)
952 || (!e
->expanded
&& expandable
&&
953 e
->index
<curr_selected
&& !(e
->index
.next()<=curr_selected
));
954 if(expandable
|| sel
) {
956 e
->fregna
= TextPtr(new Text(e
.get(), 1, this));
958 /* update the data, if needed */
959 TextPtr f
= e
->fregna
;
960 const char *text
= (sel
||!e
->expanded
||e
->hide_next
) ? "[+]" : "[-]";
961 if(f
->text
!= text
|| f
->selected
!= sel
) {
964 f
->needs_update
= true;
966 else if( !f
->showing() || layout_must_relayout
)
967 f
->needs_update
= true;
969 QPoint
dest(flow_x
, flow_y
);
973 f
->goTo(QPoint(flow_x
, flow_y
));
975 f
->moveTo(QPoint(flow_x
, flow_y
));
982 e
->fregna
->disappear();
984 /* update the flow information */
985 flow_y
+= entry_size
;
986 layout_max_width
= std::max(flow_x
, layout_max_width
);
987 int prev_pos
= flow_y
;
989 /* update the comment */
991 CommentPtr c
= e
->comment
;
993 if(e
->expanded
&& !e
->hide_next
) {
994 QPoint
dest(at_x
+ COMMENT_INDENTATION
, flow_y
);
996 if(c
->pos() != dest
) {
1001 c
->needs_update
= true;
1003 else if( !c
->showing() || layout_width_changed
|| layout_must_relayout
)
1004 c
->needs_update
= true;
1007 flow_y
+= c
->height
;
1013 /* update the variations */
1014 for(Variations::iterator it
= e
->variations
.begin(); it
!= e
->variations
.end(); ++it
) {
1015 int old_pos
= flow_y
;
1017 /* update the variation's comment */
1018 if(e
->vcomments
.count(it
->first
)) {
1019 CommentPtr c
= e
->vcomments
[it
->first
];
1021 if(e
->expanded
&& !e
->hide_next
) {
1022 QPoint
dest(at_x
+ VAR_INDENTATION
+ COMMENT_INDENTATION
, flow_y
);
1024 if( !c
->showing() || layout_must_relayout
)
1025 c
->needs_update
= true;
1026 if(c
->pos() != dest
)
1033 flow_y
+= c
->height
;
1039 /* layout the variation */
1040 flow_y
= layoutHistory(it
->second
, at_x
+ VAR_INDENTATION
, flow_y
,
1041 e
->move_turn
, mv_num
, sub_mv_num
, e
->expanded
&& !e
->hide_next
);
1043 /* update the brace of the variation */
1044 BracePtr b
= e
->braces
[it
->first
];
1045 if(e
->expanded
&& !e
->hide_next
) {
1046 b
->depth
= e
->index
.nested
.size();
1047 b
->setHeight((it
->second
.size() && it
->second
[0]->hide_next
) ? entry_size
: flow_y
- old_pos
);
1048 b
->width
= VAR_INDENTATION
;
1050 b
->goTo(QPoint(at_x
, old_pos
));
1052 b
->moveTo(QPoint(at_x
, old_pos
));
1059 e
->childs_height
= flow_y
- prev_pos
;
1063 prev_turn
= e
->move_turn
;
1068 QPixmap
Widget::getPixmap(const QString
& s
, bool selected
) {
1069 QString k
= selected
? s
+"_sel":s
;
1070 if(loaded_pixmaps
.contains(k
))
1071 return loaded_pixmaps
[k
];
1073 QString iconFile
= KStandardDirs::locate("appdata", "piece_icons/" + s
+ ".png");
1074 QImage
img(iconFile
);
1078 p
.setCompositionMode(QPainter::CompositionMode_SourceAtop
);
1079 p
.fillRect(0,0,img
.width(), img
.height(), m_settings
->select_color
);
1081 return loaded_pixmaps
[k
] = QPixmap::fromImage(img
.scaled(m_settings
->mv_fmetrics
.ascent(),
1082 m_settings
->mv_fmetrics
.ascent(),
1083 Qt::IgnoreAspectRatio
, Qt::SmoothTransformation
));
1086 void Widget::setComment(EntryPtr e
, int v
, const QString
& comment
) {
1087 if(comment
.isEmpty()) {
1090 e
->comment
= CommentPtr();
1093 if(e
->vcomments
.count(v
))
1094 e
->vcomments
.erase(v
);
1102 e
->comment
= CommentPtr(new Comment(e
.get(), this));
1106 if(!e
->variations
.count(v
))
1109 if(!e
->vcomments
.count(v
))
1110 p
= e
->vcomments
[v
] = CommentPtr(new Comment(e
.get(), this, v
));
1112 p
= e
->vcomments
[v
];
1115 if(p
->text
== comment
)
1118 p
->needs_update
= true;
1124 void Widget::setComment(const Index
& index
, const QString
& comment
) {
1125 EntryPtr e
= fetch(index
);
1127 ERROR("Invalid index " << index
);
1130 setComment(e
, -1, comment
);
1133 void Widget::setVComment(const Index
& index
, int v
, const QString
& comment
) {
1134 EntryPtr e
= fetch(index
);
1135 if(!e
|| !e
->variations
.count(v
)) {
1136 ERROR("Invalid index " << index
);
1139 setComment(e
, v
, comment
);
1142 void Widget::setMove(const Index
& index
,
1143 int turn
, const QString
& move
, const QString
& comment
) {
1146 mv
.push_back(MovePart(move
));
1148 //TODO: move this code in some other place, it really should not stay here
1149 QRegExp
reg("[KQRBNP]");
1151 while(reg
.indexIn(move
, x
) != -1) {
1153 mv
.push_back(MovePart(MoveText
, move
.mid(x
, reg
.pos()-x
)));
1154 mv
.push_back(MovePart(MovePixmap
, reg
.cap().toLower()));
1155 x
= reg
.pos() + reg
.matchedLength();
1158 mv
.push_back(MovePart(MoveText
, move
.mid(x
)));
1160 setMove(index
, turn
, mv
, comment
);
1163 void Widget::setMove(const Index
& index
,
1164 int turn
, const DecoratedMove
& move
, const QString
& comment
) {
1165 EntryPtr e
= fetch(index
);
1167 e
->move_turn
= turn
;
1169 e
->needs_update
= true;
1170 setComment(e
, -1, comment
);
1176 History
*vec
= fetchRef(index
.prev(), &at
);
1178 ERROR("Invalid index " << index
);
1182 if(index
.nested
.size() && index
.nested
.back().num_moves
== 0) {
1184 int v
= index
.nested
.back().variation
;
1185 var
.push_back(e
= EntryPtr( new Entry(turn
, move
, index
, this)) );
1186 (*vec
)[at
]->variations
[v
] = var
;
1187 (*vec
)[at
]->braces
[v
] = BracePtr( new Brace( (*vec
)[at
].get(), v
, this) );
1190 vec
->push_back(e
= EntryPtr( new Entry(turn
, move
, index
, this)) );
1192 setComment(e
, -1, comment
);
1197 void Widget::remove(const Index
& index
) {
1199 if(index
.atVariationStart() ) {
1200 EntryPtr e
= fetch(index
.prev());
1204 int v
= index
.nested
.back().variation
;
1205 if(!e
->variations
.count(v
))
1208 e
->variations
.erase(v
);
1210 e
->vcomments
.erase(v
);
1214 History
*vec
= fetchRef(index
, &at
);
1218 while((int)vec
->size() > at
)
1224 void Widget::fixIndices(const Index
& ix
) {
1226 History
*vec
= fetchRef(ix
, &at
);
1228 ERROR("Invalid index " << ix
);
1232 for(int i
=at
;i
<(int)vec
->size();i
++) {
1233 EntryPtr e
= (*vec
)[i
];
1236 for(Variations::const_iterator it
= e
->variations
.begin();
1237 it
!= e
->variations
.end(); ++it
)
1238 fixIndices(index
.next(it
->first
));
1239 index
= index
.next();
1243 void Widget::promoteVariation(const Index
& ix
, int v
) {
1245 History
*vec
= fetchRef(ix
, &at
);
1247 ERROR("Invalid index " << ix
);
1251 History vold
= (*vec
)[at
]->variations
[v
];
1253 for(int i
=at
+1; i
<(int)vec
->size(); i
++)
1254 vnew
.push_back((*vec
)[i
]);
1255 while((int)vec
->size()>at
+1)
1257 for(int i
=0; i
<(int)vold
.size(); i
++)
1258 vec
->push_back(vold
[i
]);
1259 (*vec
)[at
]->variations
[v
] = vnew
;
1261 Q_ASSERT((int)vec
->size()>at
+1);
1262 (*vec
)[at
+1]->hide_next
= false;
1264 fixIndices(ix
.next());
1265 fixIndices(ix
.next(v
));
1267 curr_selected
= curr_selected
.flipVariation(ix
, v
);
1268 curr_highlight
= curr_highlight
.flipVariation(ix
, v
);
1272 void Widget::select(const Index
& index
) {
1273 if(curr_selected
== index
)
1275 EntryPtr e
= fetch(index
);
1276 EntryPtr olde
= fetch(curr_selected
);
1278 olde
->selected
= false;
1279 olde
->needs_update
= true;
1283 e
->needs_update
= true;
1284 layout_goto_selected
= true;
1286 curr_selected
= index
;
1290 void Widget::setLoaderTheme(const ThemeInfo
& theme
) {
1291 m_loader
.setTheme(theme
);
1294 //END Widget-------------------------------------------------------------------
1296 } //end namespace MoveList