UBTBPageEditWidget.cpp 17.7 KB
Newer Older
1
#include "globals/UBGlobals.h"
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
#include "core/UBApplication.h"
#include "frameworks/UBFileSystemUtils.h"
#include "gui/UBMediaPlayer.h"
#include "customWidgets/UBMediaWidget.h"

#include "UBTBPageEditWidget.h"

UBTBPageEditWidget::UBTBPageEditWidget(UBTeacherBarDataMgr *pDataMgr, QWidget *parent, const char *name):QWidget(parent)
  , mpDataMgr(NULL)
  , mpTitleLabel(NULL)
  , mpTitle(NULL)
  , mpMediaLabel(NULL)
  , mpActionLabel(NULL)
  , mpActions(NULL)
  , mpActionButton(NULL)
  , mpLinkLabel(NULL)
  , mpLinks(NULL)
  , mpLinkButton(NULL)
  , mpCommentLabel(NULL)
  , mpComments(NULL)
  , mpDocumentEditbutton(NULL)
  , mpPagePreviewButton(NULL)
  , mpContainer(NULL)
{
    Q_UNUSED(name);
    mpDataMgr = pDataMgr;
28 29
    mActions.clear();
    mUrls.clear();
30 31
    setAttribute(Qt::WA_StyledBackground, true);
    setStyleSheet(UBApplication::globalStyleSheet());
32
    mClearingFields = false;
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
    mLayout.setContentsMargins(0, 0, 0, 0);
    setLayout(&mLayout);

    mpContainer = new QWidget(this);
    mpContainer->setObjectName("DockPaletteWidgetBox");
    mpContainer->setLayout(&mContainerLayout);
    mLayout.addWidget(mpContainer, 1);

    // Title
    mpTitleLabel = new QLabel(tr("Title"), mpContainer);
    mpTitle = new QLineEdit(mpContainer);
    mpTitle->setObjectName("DockPaletteWidgetLineEdit");
    mContainerLayout.addWidget(mpTitleLabel, 0);
    mContainerLayout.addWidget(mpTitle, 0);

    // Actions
    mpActionLabel = new QLabel(tr("Actions"), mpContainer);
    mContainerLayout.addWidget(mpActionLabel, 0);
    mpActions = new UBWidgetList(mpContainer);
    mpActions->setEmptyText(tr("Add actions"));
    mContainerLayout.addWidget(mpActions, 1);
    mpActionButton = new QPushButton(mpContainer);
    mpActionButton->setObjectName("DockPaletteWidgetButton");
    mpActionButton->setText(tr("Add action"));
    mActionLayout.addWidget(mpActionButton, 0);
    mActionLayout.addStretch(1);
    mContainerLayout.addLayout(&mActionLayout, 0);

    // Media
    mpMediaLabel = new QLabel(tr("Medias"), mpContainer);
    mContainerLayout.addWidget(mpMediaLabel, 0);
    mpMediaContainer = new UBTBMediaContainer(mpContainer);
    mpMediaContainer->setEmptyText(tr("Drop media here"));
    mContainerLayout.addWidget(mpMediaContainer, 1);

    // Links
    mpLinkLabel = new QLabel(tr("Links"), mpContainer);
    mContainerLayout.addWidget(mpLinkLabel, 0);
    mpLinks = new UBWidgetList(mpContainer);
    mContainerLayout.addWidget(mpLinks, 1);
    mpLinkButton = new QPushButton(tr("Add link"), mpContainer);
    mpLinkButton->setObjectName("DockPaletteWidgetButton");
    mLinkLayout.addWidget(mpLinkButton, 0);
    mLinkLayout.addStretch(1);
    mContainerLayout.addLayout(&mLinkLayout, 0);

    // Comments
    mpCommentLabel = new QLabel(tr("Comments"), mpContainer);
    mContainerLayout.addWidget(mpCommentLabel, 0);
    mpComments = new QTextEdit(mpContainer);
    mpComments->setObjectName("DockPaletteWidgetBox");
    mpComments->setStyleSheet("background:white;");
    mContainerLayout.addWidget(mpComments, 1);

    mpPagePreviewButton = new QPushButton(tr("Preview"), this);
    mpPagePreviewButton->setObjectName("DockPaletteWidgetButton");
    mpDocumentEditbutton = new QPushButton(tr("Document View"), this);
    mpDocumentEditbutton->setObjectName("DockPaletteWidgetButton");
    mPagePreviewLayout.addWidget(mpDocumentEditbutton, 0);
    mPagePreviewLayout.addWidget(mpPagePreviewButton, 0);
    mPagePreviewLayout.addStretch(1);
    mLayout.addLayout(&mPagePreviewLayout, 0);

96 97
    connect(mpTitle, SIGNAL(textChanged(QString)), this, SLOT(onTitleChanged()));
    connect(mpComments, SIGNAL(textChanged()), this, SLOT(onCommentsChanged()));
98 99 100 101 102
    connect(mpActionButton, SIGNAL(clicked()), this, SLOT(onActionButton()));
    connect(mpLinkButton, SIGNAL(clicked()), this, SLOT(onLinkButton()));
    connect(mpDocumentEditbutton, SIGNAL(clicked()), this, SLOT(onDocumentEditClicked()));
    connect(mpPagePreviewButton, SIGNAL(clicked()), this, SLOT(onPagePreviewClicked()));
    connect(mpMediaContainer, SIGNAL(mediaDropped(QString)), this, SLOT(onMediaDropped(QString)));
103 104 105
    connect(mpActions, SIGNAL(closeWidget(QWidget*)), this, SLOT(onCloseWidget(QWidget*)));
    connect(mpLinks, SIGNAL(closeWidget(QWidget*)), this, SLOT(onCloseWidget(QWidget*)));
    connect(mpMediaContainer, SIGNAL(closeWidget(QWidget*)), this, SLOT(onCloseWidget(QWidget*)));
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}

UBTBPageEditWidget::~UBTBPageEditWidget()
{
    DELETEPTR(mpDocumentEditbutton);
    DELETEPTR(mpPagePreviewButton);
    DELETEPTR(mpComments);
    DELETEPTR(mpCommentLabel);
    DELETEPTR(mpLinks);
    DELETEPTR(mpLinkLabel);
    DELETEPTR(mpLinkButton);
    DELETEPTR(mpMediaLabel);
    DELETEPTR(mpActionButton);
    DELETEPTR(mpActionLabel);
    DELETEPTR(mpTitleLabel);
    DELETEPTR(mpTitle);
}

124
void UBTBPageEditWidget::onTitleChanged()
125
{
126 127 128 129
    if(!mClearingFields){
        mpDataMgr->setPageTitle(mpTitle->text());
        emit valueChanged();
    }
130 131 132 133
}

void UBTBPageEditWidget::onCommentsChanged()
{
134 135 136 137
    if(!mClearingFields){
        mpDataMgr->setComments(mpComments->document()->toPlainText());
        emit valueChanged();
    }
138 139 140 141 142
}

void UBTBPageEditWidget::onActionButton()
{
    UBTeacherStudentAction* pAction = new UBTeacherStudentAction(this);
143
    mActions << pAction;
144
    mpActions->addWidget(pAction);
145
    connectActions(pAction);
146
    emit valueChanged();
147 148 149 150 151
}

void UBTBPageEditWidget::onLinkButton()
{
    UBUrlWidget* pUrl = new UBUrlWidget(this);
152
    mUrls << pUrl;
153
    mpLinks->addWidget(pUrl);
154
    connectActions(pUrl);
155
    emit valueChanged();
156 157 158 159 160 161 162
}

void UBTBPageEditWidget::onMediaDropped(const QString &url)
{
    if("" != url){
        QWidget* pMedia = mpMediaContainer->generateMediaWidget(url);
        if(NULL != pMedia){
163 164 165 166
            mMedias << pMedia;
            mMediaUrls << url;
            //mpDataMgr->medias()->append(pMedia);
            //mpDataMgr->addMediaUrl(url);
167
            mpMediaContainer->addWidget(pMedia);
168
            connectActions(pMedia);
169
            emit valueChanged();
170 171 172 173
        }
    }
}

174 175 176 177 178 179 180 181
void UBTBPageEditWidget::connectActions(QWidget* w)
{
    UBActionableWidget* pActionable = dynamic_cast<UBActionableWidget*>(w);
    if(NULL != pActionable){
        connect(pActionable, SIGNAL(close(QWidget*)), this, SLOT(onCloseWidget(QWidget*)));
    }
}

182
void UBTBPageEditWidget::onDocumentEditClicked()
183
{
184 185
    emit changeTBState(eTeacherBarState_DocumentEdit);
}
186

187 188 189 190
void UBTBPageEditWidget::onPagePreviewClicked()
{
    emit changeTBState(eTeacherBarState_PagePreview);
}
191

192 193 194 195
void UBTBPageEditWidget::saveFields()
{
    mpDataMgr->actions()->clear();
    mpDataMgr->urls()->clear();
196
    mpDataMgr->mediaUrls()->clear();
197
    mpDataMgr->medias()->clear();
198

199 200 201 202 203 204 205 206 207 208 209
    foreach(UBTeacherStudentAction* pAct, mActions){
        sAction action;
        action.type = pAct->comboValue().toInt();
        action.content = pAct->text();
        mpDataMgr->actions()->append(action);
    }
    foreach(UBUrlWidget* pUrl, mUrls){
        sLink link;
        link.title = pUrl->title();
        link.link = pUrl->url();
        mpDataMgr->urls()->append(link);
210
    }
211
    foreach(QString url, mMediaUrls){
212 213
        qDebug() << "saving media :" << url;
        mpDataMgr->mediaUrls()->append(url);
214 215 216 217
    }
    foreach(QWidget* pMedia, mMedias){
        mpDataMgr->medias()->append(pMedia);
    }
218 219
}

220
void UBTBPageEditWidget::updateFields()
221
{
222
    // Title
223
    mpTitle->setText(mpDataMgr->pageTitle());
224
    // Actions
225 226 227 228 229 230
    foreach(sAction action, *mpDataMgr->actions()){
        UBTeacherStudentAction* pAction = new UBTeacherStudentAction(this);
        pAction->setComboValue(action.type);
        pAction->setText(action.content);
        mActions << pAction;
        mpActions->addWidget(pAction);
231
        connectActions(pAction);
232
    }
233
    // Medias
234 235 236 237 238 239 240
    foreach(QString url, *mpDataMgr->mediaUrls()){
        if(!url.isEmpty()){
            mMediaUrls << url;
            QWidget* pWidget = mpMediaContainer->generateMediaWidget(url);
            if(pWidget != NULL){
                mMedias << pWidget;
                mpMediaContainer->addWidget(pWidget);
241
                connectActions(pWidget);
242
            }
243
        }
244
    }
245

246
    // Links
247 248 249 250 251 252
    foreach(sLink link, *mpDataMgr->urls()){
        UBUrlWidget* urlWidget = new UBUrlWidget(this);
        urlWidget->setTitle(link.title);
        urlWidget->setUrl(link.link);
        mUrls << urlWidget;
        mpLinks->addWidget(urlWidget);
253
        connectActions(urlWidget);
254
    }
255
    // Comments
256
    mpComments->document()->setPlainText(mpDataMgr->comments());
257 258
}

259
void UBTBPageEditWidget::clearFields()
260
{
261
    mClearingFields = true;
262
    // Title
263
    mpTitle->setText("");
264
    // Actions
265 266 267 268 269
    foreach(UBTeacherStudentAction* pAction, mActions){
        mpActions->removeWidget(pAction);
        DELETEPTR(pAction);
    }
    mActions.clear();
270
    // Medias
271
    foreach(QWidget* pMedia, mMedias){
272 273 274 275 276
        if(NULL != pMedia){
            mpMediaContainer->removeWidget(pMedia);
            DELETEPTR(pMedia);
        }
    }
277 278
    mMedias.clear();
    mMediaUrls.clear();
279
    // Links
280 281 282 283 284
    foreach(UBUrlWidget* pLink, mUrls){
        mpLinks->removeWidget(pLink);
        DELETEPTR(pLink);
    }
    mUrls.clear();
285 286
    // Comments
    mpComments->setText("");
287 288

    mClearingFields = false;
289 290
}

291 292 293 294 295 296 297 298 299 300 301 302 303
void UBTBPageEditWidget::onCloseWidget(QWidget *w)
{
    if(NULL != w){
        if("UBTeacherStudentAction" == w->objectName()){
            UBTeacherStudentAction* pW = dynamic_cast<UBTeacherStudentAction*>(w);
            mpActions->removeWidget(pW);
            mActions.remove(mActions.indexOf(pW));
            DELETEPTR(w);
        }else if("UBUrlWidget" == w->objectName()){
            UBUrlWidget* pW = dynamic_cast<UBUrlWidget*>(w);
            mpLinks->removeWidget(pW);
            mUrls.remove(mUrls.indexOf(pW));
            DELETEPTR(w);
304 305 306 307 308 309 310 311 312
        }else if("UBTBMediaPicture" == w->objectName()){
            UBPictureWidget* pW = dynamic_cast<UBPictureWidget*>(w);
            mMediaUrls.removeOne(pW->url());
            mpMediaContainer->removeWidget(w);
            mMedias.remove(mMedias.indexOf(w));
            DELETEPTR(w);
        }else if("UBMediaWidget" == w->objectName()){
            UBMediaWidget* pW = dynamic_cast<UBMediaWidget*>(w);
            mMediaUrls.removeOne(pW->url());
313 314 315 316 317 318 319
            mpMediaContainer->removeWidget(w);
            mMedias.remove(mMedias.indexOf(w));
            DELETEPTR(w);
        }
    }
}

320
// ---------------------------------------------------------------------------------------------
321
UBUrlWidget::UBUrlWidget(QWidget *parent, const char *name):UBActionableWidget(parent, name)
322 323 324 325 326 327
  , mpLayout(NULL)
  , mpUrlLabel(NULL)
  , mpUrl(NULL)
{
    setAttribute(Qt::WA_StyledBackground, true);
    setStyleSheet(UBApplication::globalStyleSheet());
328
    addAction(eAction_Close);
329 330 331 332

    mpLayout = new QVBoxLayout(this);
    setLayout(mpLayout);

333
    mpLabelLayout = new QHBoxLayout(0);
334 335 336 337 338 339 340
    mpUrlLabel = new QLabel(tr("Url"), this);
    mpLabelLayout->addWidget(mpUrlLabel, 0);
    mpUrl = new QLineEdit(this);
    mpUrl->setObjectName("DockPaletteWidgetLineEdit");
    mpUrl->setMinimumHeight(20);
    mpLabelLayout->addWidget(mpUrl, 1);

341
    mpTitleLayout = new QHBoxLayout(0);
342 343 344 345 346 347 348 349 350
    mpTitleLabel = new QLabel(tr("Title"),this);
    mpTitleLayout->addWidget(mpTitleLabel,0);
    mpTitle = new QLineEdit(this);
    mpTitle->setObjectName("DockPaletteWidgetLineEdit");
    mpTitle->setMinimumHeight(20);
    mpTitleLayout->addWidget(mpTitle,1);

    mpLayout->addLayout(mpTitleLayout);
    mpLayout->addLayout(mpLabelLayout);
351
    setActionsParent(this);
352 353 354 355
}

UBUrlWidget::~UBUrlWidget()
{
356
    unsetActionsParent();
357 358 359 360 361 362 363 364 365 366 367 368 369 370
    DELETEPTR(mpTitle);
    DELETEPTR(mpTitleLabel);
    DELETEPTR(mpUrlLabel);
    DELETEPTR(mpUrl);
    DELETEPTR(mpTitleLayout);
    DELETEPTR(mpLabelLayout);
    DELETEPTR(mpLayout);
}

QString UBUrlWidget::url()
{
    QString str;

    if(NULL != mpUrl){
371
        str = mpUrl->text();// + ";" + mpTitle->text();
372 373 374 375 376 377 378 379
    }

    return str;
}

void UBUrlWidget::setUrl(const QString &url)
{
    if(NULL != mpUrl){
380
        mpUrl->setText(url);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
    }
}

//  ------------------------------------------------------------------------------------------------------------------------------------
UBTBMediaContainer::UBTBMediaContainer(QWidget *parent, const char *name) : UBWidgetList(parent)
{
    setObjectName(name);
    setAcceptDrops(true);
}

UBTBMediaContainer::~UBTBMediaContainer()
{

}

void UBTBMediaContainer::dropEvent(QDropEvent* pEvent)
{
    QPixmap pixFromDropEvent;
    QString mimeType;
    QString resourcePath;
    if(pEvent->mimeData()->hasText()){
        resourcePath = pEvent->mimeData()->text();
    }
    else if(pEvent->mimeData()->hasUrls()){
        resourcePath = pEvent->mimeData()->urls().at(0).toLocalFile();
    }
    else if(pEvent->mimeData()->hasImage()){
        pixFromDropEvent.loadFromData(pEvent->mimeData()->imageData().toByteArray());
        if(!pixFromDropEvent.isNull())
            mimeType = "image";
    }

    if (mimeType.isEmpty() && resourcePath.isEmpty()){
        pEvent->acceptProposedAction();
        return;
    }
    if(!resourcePath.isEmpty()){
        emit mediaDropped(resourcePath);
        pEvent->acceptProposedAction();
    }
}

void UBTBMediaContainer::dragEnterEvent(QDragEnterEvent* pEvent)
{
    pEvent->acceptProposedAction();
}

void UBTBMediaContainer::dragMoveEvent(QDragMoveEvent* pEvent)
{
    pEvent->acceptProposedAction();
}

void UBTBMediaContainer::dragLeaveEvent(QDragLeaveEvent* pEvent)
{
    pEvent->accept();
}

void UBTBMediaContainer::addMedia(const QString& mediaPath)
{
    if(!mediaPath.isEmpty())
        mMediaList.append(mediaPath);
    else
        qWarning() << __FUNCTION__ <<  "empty path";

    QString mimeType = UBFileSystemUtils::mimeTypeFromFileName(mediaPath);
    if(mimeType.contains("image")){
        QPixmap pix = QPixmap(mediaPath);
        QLabel* label = new QLabel();
        label->setPixmap(pix);
        label->setScaledContents(true);
        addWidget(label);
    }
    else if(mimeType.contains("video") || mimeType.contains("audio")){
        UBMediaPlayer* mediaPlayer = new UBMediaPlayer();
        mediaPlayer->setFile(mediaPath);
        addWidget(mediaPlayer);
    }
    else{
        qWarning() << "pMediaPath" << mediaPath;
        qWarning() << "bad idea to come here";
    }
}

QStringList UBTBMediaContainer::mediaUrls()
{
    return mMediaList;
}

void UBTBMediaContainer::cleanMedias()
{
    mMediaList.clear();
}

QWidget* UBTBMediaContainer::generateMediaWidget(const QString& url)
{
    QWidget* pW = NULL;

    if(!url.isEmpty())
        mMediaList.append(url);
    else
        qWarning() << __FUNCTION__ <<  "empty path";

    QString mimeType = UBFileSystemUtils::mimeTypeFromFileName(url);
    if(mimeType.contains("image")){
        QPixmap pix = QPixmap(url);
486
        UBPictureWidget* pic = new UBPictureWidget();
487
        pic->setUrl(url);
488 489 490 491 492 493
        pix.scaledToWidth(pic->label()->width());
        pic->label()->resize(pix.width(), pix.height());
        pic->label()->setPixmap(pix);
        pic->label()->setScaledContents(true);
        pic->setObjectName("UBTBMediaPicture");
        pW = pic;
494 495 496 497
    }
    else if(mimeType.contains("video") || mimeType.contains("audio")){
        UBMediaWidget* mediaPlayer = new UBMediaWidget(mimeType.contains("audio")?eMediaType_Audio:eMediaType_Video);
        mediaPlayer->setFile(url);
498
        mediaPlayer->setUrl(url);
499 500 501 502 503 504 505 506 507
        pW = mediaPlayer;
    }
    else{
        qWarning() << "pMediaPath" << url;
        qWarning() << "bad idea to come here";
    }

    return pW;
}
508

509
UBTeacherStudentAction::UBTeacherStudentAction(QWidget *parent, const char *name):UBActionableWidget(parent, name)
510 511 512 513 514 515 516
  , mpText(NULL)
  , mpLayout(NULL)
  , mpComboLayout(NULL)
  , mpCombo(NULL)
{
    setAttribute(Qt::WA_StyledBackground, true);
    setStyleSheet(UBApplication::globalStyleSheet());
517
    addAction(eAction_Close);
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

    // Create the GUI
    mpLayout = new QHBoxLayout(this);
    setLayout(mpLayout);

    mpComboLayout = new QVBoxLayout();

    mpCombo = new QComboBox(this);
    mpCombo->setObjectName("DockPaletteWidgetComboBox");
    mpCombo->setMinimumWidth(80);
    mpCombo->addItem(tr("Teacher"));
    mpCombo->addItem(tr("Student"));
    mpComboLayout->addWidget(mpCombo, 0);
    mpComboLayout->addStretch(1);

    mpLayout->addLayout(mpComboLayout, 0);

    mpText = new QTextEdit(this);
    mpText->setObjectName("DockPaletteWidgetBox");
    mpText->setStyleSheet("background:white;");

    mpLayout->addWidget(mpText, 1);
540
    setActionsParent(this);
541 542 543 544
}

UBTeacherStudentAction::~UBTeacherStudentAction()
{
545
    unsetActionsParent();
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
    DELETEPTR(mpCombo);
    DELETEPTR(mpText);
    DELETEPTR(mpComboLayout);
    DELETEPTR(mpLayout);
}

QString UBTeacherStudentAction::text()
{
    QString str;
    if(NULL != mpText){
        str = mpText->document()->toPlainText();
    }
    return str;
}

QString UBTeacherStudentAction::comboValue()
{
    QString str;

    if(NULL != mpCombo){
        str = QString("%0").arg(mpCombo->currentIndex());
    }

    return str;
}

void UBTeacherStudentAction::setComboValue(int value)
{
    if(NULL != mpCombo){
        mpCombo->setCurrentIndex(value);
    }
}

void UBTeacherStudentAction::setText(const QString& text)
{
    if(NULL != mpText){
        mpText->document()->setPlainText(text);
    }
}

586 587 588 589 590 591 592 593 594 595 596 597
// -------------------------------------------------------------
UBPictureWidget::UBPictureWidget(QWidget *parent, const char *name):UBActionableWidget(parent, name)
  , mpLayout(NULL)
  , mpLabel(NULL)
{
    addAction(eAction_Close);
    mpLayout = new QVBoxLayout(this);
    setLayout(mpLayout);
    mpLayout->setContentsMargins(10, 0, 10, 0);
    mpLabel = new QLabel(this);
    mpLayout->addWidget(mpLabel);
    mpLabel->setGeometry( 10, 10, width()-2*10, height());
598
    setActionsParent(mpLabel);
599 600 601 602
}

UBPictureWidget::~UBPictureWidget()
{
603
    unsetActionsParent();
604 605 606 607 608 609
    DELETEPTR(mpLabel);
    DELETEPTR(mpLayout);
}

void UBPictureWidget::resizeEvent(QResizeEvent *ev)
{
610
    Q_UNUSED(ev);
611 612
    mpLabel->setGeometry( 10, 10, width()-2*10, height());
}