00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "commentsview.h"
00021 #include "commentsviewitem.h"
00022 #include <models/commentsmodel.h>
00023 #include "newcommentform.h"
00024
00025 #include <Plasma/ItemBackground>
00026 #include <Plasma/LineEdit>
00027 #include <Plasma/Frame>
00028 #include <Plasma/ScrollWidget>
00029
00030 #include <QTreeView>
00031 #include <QGraphicsLinearLayout>
00032 #include <QGraphicsProxyWidget>
00033
00034 CommentsView::CommentsView( QGraphicsItem* parent, Qt::WindowFlags wFlags )
00035 : AbstractItemView( parent, wFlags )
00036 , m_itemBackground(new Plasma::ItemBackground( this ))
00037 , m_rootWidget( 0 )
00038 , m_commentsFrame(new Plasma::Frame( this ))
00039 , m_commentsLayout(new QGraphicsLinearLayout( Qt::Vertical, m_commentsFrame ))
00040 , m_isOnline( false )
00041 {
00042 m_commentsFrame->setLayout( m_commentsLayout );
00043 m_contentLayout->addItem( m_commentsFrame );
00044 }
00045
00046 void CommentsView::setModel( QAbstractItemModel* model )
00047 {
00048 AbstractItemView::setModel( model );
00049 connect( model, SIGNAL( modelReset() ), SLOT( reloadComments() ) );
00050
00051 m_rootWidget = new QGraphicsWidget( m_commentsFrame );
00052 for( int i = 0; i < m_model->rowCount(); ++i )
00053 {
00054 addComment( m_model->index( i, 0 ), m_rootWidget, 0 );
00055 }
00056 }
00057
00058 CommentsViewItem* CommentsView::addComment( const QModelIndex& index, QGraphicsWidget* parent, int depth )
00059 {
00060 CommentsViewItem* item = new CommentsViewItem( parent );
00061 item->setReplyEnabled( qobject_cast<GluonPlayer::CommentsModel*>( m_model )->isOnline() );
00062 item->setParent( parent );
00063 item->setDepth( depth );
00064 item->setModelIndex( index );
00065 item->setAcceptHoverEvents( true );
00066 item->installEventFilter( this );
00067 connect( item, SIGNAL( replyClicked() ), SLOT( showReply() ) );
00068 item->setRowInLayout( m_commentsLayout->count() );
00069 m_commentsLayout->addItem( item );
00070
00071 if( m_model->hasChildren( index ) )
00072 {
00073 for( int i = 0; i < m_model->rowCount( index ); ++i )
00074 {
00075 addComment( index.child( i, 0 ), item, depth + 1 );
00076 }
00077 }
00078
00079 return item;
00080 }
00081
00082 bool CommentsView::eventFilter( QObject* obj, QEvent* event )
00083 {
00084 if( event->type() == QEvent::GraphicsSceneHoverEnter )
00085 {
00086 QGraphicsItem* item = qobject_cast<QGraphicsItem*> ( obj );
00087 m_itemBackground->setTargetItem( item );
00088 }
00089
00090 return QObject::eventFilter( obj, event );
00091 }
00092
00093 void CommentsView::showReply()
00094 {
00095 CommentsViewItem* parentItem = qobject_cast<CommentsViewItem*>( sender() );
00096
00097 hideComments();
00098 NewCommentForm* form = new NewCommentForm( this );
00099 m_contentLayout->addItem( form );
00100 form->setParentIndex( parentItem->modelIndex() );
00101
00102 connect( form, SIGNAL( accepted( QModelIndex, QString, QString ) ),
00103 SLOT( addNewUserComment( QModelIndex, QString, QString ) ) );
00104 connect( form, SIGNAL( canceled() ), SLOT( cancelNewComment() ) );
00105 }
00106
00107 void CommentsView::removeComments()
00108 {
00109 CommentsViewItem* toDelete;
00110
00111 while( m_commentsLayout->count() > 0 )
00112 {
00113 toDelete = dynamic_cast<CommentsViewItem*>( m_commentsLayout->itemAt( 0 ) );
00114 m_commentsLayout->removeAt( 0 );
00115 toDelete->deleteLater();
00116 }
00117 }
00118
00119 void CommentsView::loadComments()
00120 {
00121 for( int i = 0; i < m_model->rowCount(); ++i )
00122 {
00123 addComment( m_model->index( i, 0 ), m_rootWidget, 0 );
00124 }
00125 }
00126
00127 void CommentsView::reloadComments()
00128 {
00129 hideComments();
00130 removeComments();
00131 loadComments();
00132 showComments();
00133 }
00134
00135 void CommentsView::addNewUserComment( QModelIndex parentIndex, QString title, QString body )
00136 {
00137 GluonPlayer::CommentsModel* model = static_cast<GluonPlayer::CommentsModel*>( m_model );
00138 model->uploadComment( parentIndex, title, body );
00139 connect( model, SIGNAL( addCommentFailed() ), SLOT( showComments() ) );
00140 sender()->deleteLater();
00141 }
00142
00143 void CommentsView::cancelNewComment()
00144 {
00145 sender()->deleteLater();
00146 showComments();
00147 }
00148
00149 void CommentsView::hideComments()
00150 {
00151 m_commentsFrame->hide();
00152 m_contentLayout->removeItem( m_commentsFrame );
00153 }
00154
00155 void CommentsView::showComments()
00156 {
00157 m_contentLayout->addItem( m_commentsFrame );
00158 m_commentsFrame->show();
00159 }