UBFeaturesWidget.cpp 44.5 KB
Newer Older
1
#include <QDomDocument>
2 3
#include <QWebView>

Anna Udovichenko's avatar
Anna Udovichenko committed
4 5 6 7 8
#include "UBFeaturesWidget.h"
#include "gui/UBThumbnailWidget.h"
#include "frameworks/UBFileSystemUtils.h"
#include "core/UBApplication.h"
#include "core/UBDownloadManager.h"
9
#include "globals/UBGlobals.h"
10
#include "board/UBBoardController.h"
11 12 13

const char *UBFeaturesWidget::objNamePathList = "PathList";
const char *UBFeaturesWidget::objNameFeatureList = "FeatureList";
Anna Udovichenko's avatar
Anna Udovichenko committed
14

15 16 17
const QString UBFeaturesNewFolderDialog::acceptText = tr("Accept");
const QString UBFeaturesNewFolderDialog::cancelText = tr("Cancel");
const QString UBFeaturesNewFolderDialog::labelText =  tr("Enter a new folder name");
18 19 20 21 22 23

const QMargins FeatureListMargins(0, 0, 0, 30);
const int FeatureListBorderOffset = 10;
const char featureTypeSplitter = ':';
static const QString mimeSankoreFeatureTypes = "Sankore/featureTypes";

Aleksei Kanash's avatar
Aleksei Kanash committed
24 25 26
UBFeaturesWidget::UBFeaturesWidget(QWidget *parent, const char *name)
    : UBDockPaletteWidget(parent)
    , imageGatherer(NULL)
Anna Udovichenko's avatar
Anna Udovichenko committed
27 28 29 30 31
{
    setObjectName(name);
    mName = "FeaturesWidget";
    mVisibleState = true;

32
    SET_STYLE_SHEET();
Anna Udovichenko's avatar
Anna Udovichenko committed
33 34 35 36 37

    mIconToLeft = QPixmap(":images/library_open.png");
    mIconToRight = QPixmap(":images/library_close.png");
    setAcceptDrops(true);

38 39 40 41 42 43 44 45 46 47
    //Main UBFeature functionality
    controller = new UBFeaturesController(this);

    //Main layout including all the widgets in palette
    layout = new QVBoxLayout(this);

    //Path icon view on the top of the palette
    pathListView = new UBFeaturesListView(this, objNamePathList);
    controller->assignPathListView(pathListView);

48 49 50
    centralWidget = new UBFeaturesCentralWidget(this);
    controller->assignFeaturesListVeiw(centralWidget->listView());
    centralWidget->setSliderPosition(UBSettings::settings()->featureSliderPosition->get().toInt());
51 52 53 54 55

    //Bottom actionbar for DnD, quick search etc
    mActionBar = new UBFeaturesActionBar(controller, this);
		
    //Filling main layout
56
    layout->addWidget(pathListView);
57
    layout->addWidget(centralWidget);
58 59
    layout->addWidget(mActionBar);

60
    connect(centralWidget->listView(), SIGNAL(clicked(const QModelIndex &)), this, SLOT(currentSelected(const QModelIndex &)));
61
    connect(this, SIGNAL(sendFileNameList(QStringList)), centralWidget, SIGNAL(sendFileNameList(QStringList)));
62 63 64 65 66 67 68 69 70
    connect(mActionBar, SIGNAL(searchElement(const QString &)), this, SLOT( searchStarted(const QString &)));
    connect(mActionBar, SIGNAL(newFolderToCreate()), this, SLOT(createNewFolder()));
    connect(mActionBar, SIGNAL(deleteElements(const UBFeaturesMimeData *)), this, SLOT(deleteElements(const UBFeaturesMimeData *)));
    connect(mActionBar, SIGNAL(deleteSelectedElements()), this, SLOT(deleteSelectedElements()));
    connect(mActionBar, SIGNAL(addToFavorite(const UBFeaturesMimeData *)), this, SLOT(addToFavorite(const UBFeaturesMimeData *)));
    connect(mActionBar, SIGNAL(removeFromFavorite(const UBFeaturesMimeData *)), this, SLOT(removeFromFavorite(const UBFeaturesMimeData *)));
    connect(mActionBar, SIGNAL(addElementsToFavorite() ), this, SLOT ( addElementsToFavorite()) );
    connect(mActionBar, SIGNAL(removeElementsFromFavorite()), this, SLOT (removeElementsFromFavorite()));

71
    connect(mActionBar, SIGNAL(rescanModel()), this, SLOT(rescanModel()));
72
    connect(pathListView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(currentSelected(const QModelIndex &)));
73 74 75
    connect(UBApplication::boardController, SIGNAL(displayMetadata(QMap<QString,QString>)), this, SLOT(onDisplayMetadata( QMap<QString,QString>)));
    connect(UBDownloadManager::downloadManager(), SIGNAL( addDownloadedFileToLibrary( bool, QUrl, QString, QByteArray))
             , this, SLOT(onAddDownloadedFileToLibrary(bool, QUrl, QString,QByteArray)));
76 77 78 79
    connect(centralWidget, SIGNAL(lockMainWidget(bool)), this, SLOT(lockIt(bool)));
    connect(centralWidget, SIGNAL(createNewFolderSignal(QString)), controller, SLOT(addNewFolder(QString)));
    connect(controller, SIGNAL(scanStarted()), centralWidget, SLOT(scanStarted()));
    connect(controller, SIGNAL(scanFinished()), centralWidget, SLOT(scanFinished()));
80 81
    connect(controller, SIGNAL(scanStarted()), mActionBar, SLOT(lockIt()));
    connect(controller, SIGNAL(scanFinished()), mActionBar, SLOT(unlockIt()));
82
    connect(controller, SIGNAL(maxFilesCountEvaluated(int)), centralWidget, SIGNAL(maxFilesCountEvaluated(int)));
83 84 85
    connect(controller, SIGNAL(featureAddedFromThread()), centralWidget, SIGNAL(increaseStatusBarValue()));
    connect(controller, SIGNAL(scanCategory(QString)), centralWidget, SIGNAL(scanCategory(QString)));
    connect(controller, SIGNAL(scanPath(QString)), centralWidget, SIGNAL(scanPath(QString)));
Anna Udovichenko's avatar
Anna Udovichenko committed
86 87
}

88 89
UBFeaturesWidget::~UBFeaturesWidget()
{
90 91
    if (NULL != imageGatherer)
        delete imageGatherer;
92 93
}

94
void UBFeaturesWidget::searchStarted(const QString &pattern)
Anna Udovichenko's avatar
Anna Udovichenko committed
95
{
96
    controller->searchStarted(pattern, centralWidget->listView());
Anna Udovichenko's avatar
Anna Udovichenko committed
97 98 99 100
}

void UBFeaturesWidget::currentSelected(const QModelIndex &current)
{
101 102 103 104
    if (!current.isValid()) {
        qWarning() << "SLOT:currentSelected, invalid index catched";
        return;
    }
105

106
    QString objName = sender()->objectName();
Anna Udovichenko's avatar
Anna Udovichenko committed
107

108
    if (objName.isEmpty()) {
Claudio Valerio's avatar
Claudio Valerio committed
109
        qWarning() << "incorrect sender";
110 111 112
    } else if (objName == objNamePathList) {
        //Calling to reset the model for listView. Maybe separate function needed
        controller->searchStarted("", centralWidget->listView());
113
    }
Anna Udovichenko's avatar
Anna Udovichenko committed
114

115
    UBFeature feature = controller->getFeature(current, objName);
116 117 118 119 120 121 122

    if ( feature.isFolder() ) {
        QString newPath = feature.getFullVirtualPath();

        controller->setCurrentElement(feature);
        controller->siftElements(newPath);

123
        centralWidget->switchTo(UBFeaturesCentralWidget::MainList);
124 125 126 127 128 129 130 131

        if ( feature.getType() == FEATURE_FAVORITE ) {
            mActionBar->setCurrentState( IN_FAVORITE );

        }  else   if ( feature.getType() == FEATURE_CATEGORY && feature.getName() == "root" ) {
            mActionBar->setCurrentState( IN_ROOT );

        } else if (feature.getType() == FEATURE_TRASH) {
132
            mActionBar->setCurrentState(IN_TRASH);
133 134

        } else {
135
            mActionBar->setCurrentState(IN_FOLDER);
136 137
        }

138 139
    } else if (feature.getType() == FEATURE_SEARCH) {
        centralWidget->showElement(feature, UBFeaturesCentralWidget::FeaturesWebView);
140 141

    } else {
142
        centralWidget->showElement(feature, UBFeaturesCentralWidget::FeaturePropertiesList);
143 144
        mActionBar->setCurrentState( IN_PROPERTIES );
    }
145
    mActionBar->cleanText();
Anna Udovichenko's avatar
Anna Udovichenko committed
146 147
}

148 149
void UBFeaturesWidget::createNewFolder()
{
150
    centralWidget->showAdditionalData(UBFeaturesCentralWidget::NewFolderDialog, UBFeaturesCentralWidget::Modal);
151
    emit sendFileNameList(controller->getFileNamesInFolders());
Aleksei Kanash's avatar
Aleksei Kanash committed
152 153
}

154
void UBFeaturesWidget::deleteElements( const UBFeaturesMimeData * mimeData )
155
{
156 157 158 159
    if (!mimeData->features().count() )
        return;

    QList<UBFeature> featuresList = mimeData->features();
160
	
161 162 163 164 165 166 167 168 169 170
    foreach ( UBFeature curFeature, featuresList ) {
        if ( curFeature.inTrash()) {
            controller->deleteItem(curFeature.getFullPath());

        } else {
           controller->moveToTrash(curFeature);
        }
    }

    controller->refreshModels();
171 172
}

173 174
void UBFeaturesWidget::deleteSelectedElements()
{
175
    QModelIndexList selected = centralWidget->listView()->selectionModel()->selectedIndexes();
176

177
    foreach ( QModelIndex sel, selected )
178
    {
179 180 181 182 183 184 185 186
        UBFeature feature = sel.data(Qt::UserRole + 1).value<UBFeature>();
        if (feature.isDeletable()) {
            if (feature.inTrash()) {
                controller->deleteItem(feature);
            } else {
                controller->moveToTrash(feature, true);
            }
        }
187
    }
188
   controller->refreshModels();
189 190
}

191
void UBFeaturesWidget::rescanModel()
192
{
193
    controller->rescanModel();
194 195
}

196 197 198 199 200 201 202 203
void UBFeaturesWidget::lockIt(bool pLock)
{
    mActionBar->setEnabled(!pLock);
    pathListView->setEnabled(!pLock);
    centralWidget->setLockedExcludingAdditional(pLock);
//    pathListView->setLocked(true);
}

204
void UBFeaturesWidget::addToFavorite( const UBFeaturesMimeData * mimeData )
205
{
206 207 208 209 210 211 212 213 214
    if ( !mimeData->hasUrls() )
        return;

    QList<QUrl> urls = mimeData->urls();
    foreach ( QUrl url, urls ) {
        controller->addToFavorite(url);
    }

    controller->refreshModels();
215 216
}

217
void UBFeaturesWidget::removeFromFavorite( const UBFeaturesMimeData * mimeData )
218
{
219 220 221 222 223 224 225 226
    if ( !mimeData->hasUrls() )
        return;

    QList<QUrl> urls = mimeData->urls();

    foreach( QUrl url, urls ) {
        controller->removeFromFavorite(url);
    }
227 228
}

229 230
void UBFeaturesWidget::onDisplayMetadata( QMap<QString,QString> metadata )
{
231 232
    QString previewImageUrl;

233
    switch (static_cast<int>(UBFileSystemUtils::mimeTypeFromUrl(QUrl(metadata["Url"]))))
234 235 236 237 238
    {
    case UBMimeType::RasterImage:
    case UBMimeType::VectorImage:
        {
            previewImageUrl = ":images/libpalette/loading.png";
239

240 241
            if (!imageGatherer)
                imageGatherer = new UBDownloadHttpFile(0, this);
242

243
            connect(imageGatherer, SIGNAL(downloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(onPreviewLoaded(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)));
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
            // We send here the request and store its reply in order to be able to cancel it if needed
            imageGatherer->get(QUrl(metadata["Url"]), QPoint(0,0), QSize(), false);
        } break;
    case UBMimeType::Audio:
        {
           previewImageUrl = ":images/libpalette/soundIcon.svg";
        }break;
    case UBMimeType::Video:
        {
            previewImageUrl = ":images/libpalette/movieIcon.svg";
        }break;
    case UBMimeType::Flash:
        {
            previewImageUrl = ":images/libpalette/FlashIcon.svg";
        }break;
    }
261

Ilia Ryabokon's avatar
Ilia Ryabokon committed
262
    UBFeature feature( "/root", QImage(previewImageUrl), QString(), metadata["Url"], FEATURE_ITEM );
263
    feature.setMetadata( metadata );
264

265
    centralWidget->showElement(feature, UBFeaturesCentralWidget::FeaturePropertiesList);
266
    mActionBar->setCurrentState( IN_PROPERTIES );
267 268
}

269 270 271

void UBFeaturesWidget::onPreviewLoaded(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground)
{
272 273 274 275 276
    Q_UNUSED(id);
    Q_UNUSED(pSuccess);
    Q_UNUSED(isBackground);
    Q_UNUSED(pSize);
    Q_UNUSED(pPos);
277 278
    Q_UNUSED(sourceUrl);
    Q_UNUSED(pContentTypeHeader)
279

280 281 282
    QImage img;
    img.loadFromData(pData);
    QPixmap pix = QPixmap::fromImage(img);
283 284
    centralWidget->setPropertiesPixmap(pix);
    centralWidget->setPropertiesThumbnail(pix);
285 286
}

287 288
void UBFeaturesWidget::onAddDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentHeader, QByteArray pData)
{
289 290
    Q_UNUSED(pContentHeader)

291 292 293
    if (pSuccess) {
        controller->addDownloadedFile(sourceUrl, pData);
        controller->refreshModels();
294 295 296
    }
}

297 298
void UBFeaturesWidget::addElementsToFavorite()
{
299 300
    if ( centralWidget->currentView() == UBFeaturesCentralWidget::FeaturePropertiesList ) {
        UBFeature feature = centralWidget->getCurElementFromProperties();
301 302
        if ( feature != UBFeature() && !UBApplication::isFromWeb(feature.getFullPath().toString())) {
            controller->addToFavorite( feature.getFullPath() );
303
        }
304

305 306
    } else if ( centralWidget->currentView() == UBFeaturesCentralWidget::MainList ) {
        QModelIndexList selected = centralWidget->listView()->selectionModel()->selectedIndexes();
307
        for ( int i = 0; i < selected.size(); ++i ) {
308
            UBFeature feature = selected.at(i).data( Qt::UserRole + 1 ).value<UBFeature>();
309 310
            controller->addToFavorite(feature.getFullPath());
       }
311
    }
312 313

    controller->refreshModels();
314 315
}

316 317
void UBFeaturesWidget::removeElementsFromFavorite()
{
318
    QModelIndexList selected = centralWidget->listView()->selectionModel()->selectedIndexes();
319
    QList <QUrl> items;
320
    for ( int i = 0; i < selected.size(); ++i )  {
321 322 323
        UBFeature feature = selected.at(i).data( Qt::UserRole + 1 ).value<UBFeature>();
        items.append( feature.getFullPath() );
    }
324 325 326

    foreach ( QUrl url, items )  {
        controller->removeFromFavorite(url, true);
327
    }
328 329

    controller->refreshModels();
330 331
}

Anna Udovichenko's avatar
Anna Udovichenko committed
332 333
void UBFeaturesWidget::switchToListView()
{
334 335
//	stackedWidget->setCurrentIndex(ID_LISTVIEW);
//	currentStackedWidget = ID_LISTVIEW;
Anna Udovichenko's avatar
Anna Udovichenko committed
336 337 338 339
}

void UBFeaturesWidget::switchToProperties()
{
340 341
//	stackedWidget->setCurrentIndex(ID_PROPERTIES);
//	currentStackedWidget = ID_PROPERTIES;
Anna Udovichenko's avatar
Anna Udovichenko committed
342 343
}

344 345
void UBFeaturesWidget::switchToWebView()
{
346 347
//	stackedWidget->setCurrentIndex(ID_WEBVIEW);
//	currentStackedWidget = ID_WEBVIEW;
348
}
Anna Udovichenko's avatar
Anna Udovichenko committed
349

350 351 352 353
QStringList UBFeaturesMimeData::formats() const
{
    return QMimeData::formats();
}
354

355 356 357 358
void UBFeaturesWidget::importImage(const QImage &image, const QString &fileName)
{
    controller->importImage(image, fileName);
}
Anna Udovichenko's avatar
Anna Udovichenko committed
359

Anna Udovichenko's avatar
Anna Udovichenko committed
360
UBFeaturesListView::UBFeaturesListView( QWidget* parent, const char* name ) 
361
    : QListView(parent)
362
{
363
    setObjectName(name);
364 365
}

366 367
void UBFeaturesListView::dragEnterEvent( QDragEnterEvent *event )
{
368
    if ( event->mimeData()->hasUrls() || event->mimeData()->hasImage() )
369 370 371
		event->acceptProposedAction();
}

372 373
void UBFeaturesListView::dragMoveEvent( QDragMoveEvent *event )
{
374 375 376 377 378 379 380 381
    const UBFeaturesMimeData *fMimeData = qobject_cast<const UBFeaturesMimeData*>(event->mimeData());
    QModelIndex index = indexAt(event->pos());
    UBFeature onFeature = model()->data(index, Qt::UserRole + 1).value<UBFeature>();
    if (fMimeData) {
        if (!index.isValid() || !onFeature.isFolder()) {
            event->ignore();
            return;
        }
382 383 384 385 386 387
        foreach (UBFeature curFeature, fMimeData->features()) {
            if (curFeature == onFeature) {
                event->ignore();
                return;
            }
        }
388 389 390
     }

    if ( event->mimeData()->hasUrls() || event->mimeData()->hasImage() ) {
391
        event->acceptProposedAction();
392 393 394
    } else {
        event->ignore();
    }
395 396
}

397 398
void UBFeaturesListView::dropEvent( QDropEvent *event )
{
399
    QWidget *eventSource = event->source();
400
    if (eventSource && eventSource->objectName() == UBFeaturesWidget::objNameFeatureList) {
401 402 403 404 405 406 407 408 409 410 411 412
        event->setDropAction( Qt::MoveAction );
    }

    QListView::dropEvent( event );
}

void UBFeaturesListView::thumbnailSizeChanged( int value )
{
    setIconSize(QSize(value, value));
    setGridSize(QSize(value + 20, value + 20 ));

    UBSettings::settings()->featureSliderPosition->set(value);
413 414
}

415 416 417 418
UBFeaturesNavigatorWidget::UBFeaturesNavigatorWidget(QWidget *parent, const char *name) :
    QWidget(parent), mListView(0), mListSlder(0)

{
419
    name = "UBFeaturesNavigatorWidget";
420 421

    setObjectName(name);
422
//    SET_STYLE_SHEET()
423

424
    mListView = new UBFeaturesListView(this, UBFeaturesWidget::objNameFeatureList);
425 426 427 428 429 430 431 432 433 434 435 436 437

    mListSlder = new QSlider(Qt::Horizontal, this);

    mListSlder->setMinimum(UBFeaturesWidget::minThumbnailSize);
    mListSlder->setMaximum(UBFeaturesWidget::maxThumbnailSize);
    mListSlder->setValue(UBFeaturesWidget::minThumbnailSize);
    mListSlder->setMinimumHeight(20);

    mListView->setParent(this);
    QVBoxLayout *mainLayer = new QVBoxLayout(this);

    mainLayer->addWidget(mListView, 1);
    mainLayer->addWidget(mListSlder, 0);
438
    mainLayer->setMargin(0);
439 440 441 442 443 444 445 446

    connect(mListSlder, SIGNAL(valueChanged(int)), mListView, SLOT(thumbnailSizeChanged(int)));
}

void UBFeaturesNavigatorWidget::setSliderPosition(int pValue)
{
    mListSlder->setValue(pValue);
}
447

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
UBFeaturesCentralWidget::UBFeaturesCentralWidget(QWidget *parent) : QWidget(parent)
{
    setObjectName("UBFeaturesCentralWidget");
    SET_STYLE_SHEET();

    QVBoxLayout *mLayout = new QVBoxLayout(this);
    setLayout(mLayout);

    //Maintains the view of the main part of the palette. Consists of
    //mNavigator
    //featureProperties
    //webVeiw
    mStackedWidget = new QStackedWidget(this);

    //Main features icon view with QSlider on the bottom
    mNavigator = new UBFeaturesNavigatorWidget(this);

    //Specifies the properties of a standalone element
    mFeatureProperties = new UBFeatureProperties(this);

    //Used to show search bar on the search widget
    webView = new UBFeaturesWebView(this);

Claudio Valerio's avatar
Claudio Valerio committed
471 472 473 474 475 476
    //filling stackwidget
    mStackedWidget->addWidget(mNavigator);
    mStackedWidget->addWidget(mFeatureProperties);
    mStackedWidget->addWidget(webView);
    mStackedWidget->setCurrentIndex(MainList);
    mStackedWidget->setContentsMargins(0, 0, 0, 0);
477 478 479 480 481 482 483 484 485 486 487 488


    mAdditionalDataContainer = new QStackedWidget(this);
    mAdditionalDataContainer->setObjectName("mAdditionalDataContainer");

    //New folder dialog
    UBFeaturesNewFolderDialog *dlg = new UBFeaturesNewFolderDialog(mAdditionalDataContainer);
    mAdditionalDataContainer->addWidget(dlg);
    mAdditionalDataContainer->setCurrentIndex(NewFolderDialog);

    connect(dlg, SIGNAL(createNewFolder(QString)), this, SLOT(createNewFolderSlot(QString)));
    connect(dlg, SIGNAL(closeDialog()), this, SLOT(hideAdditionalData()));
489
    connect(this, SIGNAL(sendFileNameList(QStringList)), dlg, SLOT(setFileNameList(QStringList)));
490 491

    //Progress bar to show scanning progress
492
    UBFeaturesProgressInfo *progressBar = new UBFeaturesProgressInfo();
493 494 495
    mAdditionalDataContainer->addWidget(progressBar);
    mAdditionalDataContainer->setCurrentIndex(ProgressBarWidget);

496 497 498 499
    connect(this, SIGNAL(maxFilesCountEvaluated(int)), progressBar, SLOT(setProgressMax(int)));
    connect(this, SIGNAL(increaseStatusBarValue()), progressBar, SLOT(increaseProgressValue()));
    connect(this, SIGNAL(scanCategory(QString)), progressBar, SLOT(setCommmonInfoText(QString)));
    connect(this, SIGNAL(scanPath(QString)), progressBar, SLOT(setDetailedInfoText(QString)));
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

    mLayout->addWidget(mStackedWidget, 1);
    mLayout->addWidget(mAdditionalDataContainer, 0);

    mAdditionalDataContainer->hide();
}

void UBFeaturesCentralWidget::showElement(const UBFeature &feature, StackElement pView)
{
    if (pView == FeaturesWebView) {
        webView->showElement(feature);
        mStackedWidget->setCurrentIndex(FeaturesWebView);
    } else if (pView == FeaturePropertiesList) {
        mFeatureProperties->showElement(feature);
        mStackedWidget->setCurrentIndex(FeaturePropertiesList);
    }
}

void UBFeaturesCentralWidget::switchTo(StackElement pView)
{
    mStackedWidget->setCurrentIndex(pView);
}

void UBFeaturesCentralWidget::setPropertiesPixmap(const QPixmap &pix)
{
    mFeatureProperties->setOrigPixmap(pix);
}

void UBFeaturesCentralWidget::setPropertiesThumbnail(const QPixmap &pix)
{
    mFeatureProperties->setThumbnail(pix);
}

UBFeature UBFeaturesCentralWidget::getCurElementFromProperties()
{
Claudio Valerio's avatar
Claudio Valerio committed
535
	return mFeatureProperties->getCurrentElement();
536 537 538 539
}

void UBFeaturesCentralWidget::showAdditionalData(AddWidget pWidgetType, AddWidgetState pState)
{
540 541 542 543 544
    if (!mAdditionalDataContainer->widget(pWidgetType)) {
        qDebug() << "can't find widget specified by UBFeaturesCentralWidget::showAdditionalData(AddWidget pWidgetType, AddWidgetState pState)";
        return;
    }

545 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
    mAdditionalDataContainer->setMaximumHeight(mAdditionalDataContainer->widget(pWidgetType)->sizeHint().height());

    mAdditionalDataContainer->setCurrentIndex(pWidgetType);
    mAdditionalDataContainer->show();
    emit lockMainWidget(pState == Modal ? true : false);
}

void UBFeaturesCentralWidget::setLockedExcludingAdditional(bool pLock)
{
//    Lock all the members excluding mAdditionalDataContainer
    mStackedWidget->setEnabled(!pLock);
}

void UBFeaturesCentralWidget::createNewFolderSlot(QString pStr)
{
    emit createNewFolderSignal(pStr);
    hideAdditionalData();
}

void UBFeaturesCentralWidget::hideAdditionalData()
{
    emit lockMainWidget(false);
    mAdditionalDataContainer->hide();
}

void UBFeaturesCentralWidget::scanStarted()
{
572
    showAdditionalData(ProgressBarWidget);
573 574 575 576
}

void UBFeaturesCentralWidget::scanFinished()
{
577
    hideAdditionalData();
578 579 580 581 582 583
}

UBFeaturesNewFolderDialog::UBFeaturesNewFolderDialog(QWidget *parent) : QWidget(parent)
{
    this->setStyleSheet("background:white;");

584
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
585

586
    QVBoxLayout *labelLayout = new QVBoxLayout();
587 588 589 590

    QLabel *mLabel = new QLabel(labelText, this);
    mLineEdit = new QLineEdit(this);

Ilia Ryabokon's avatar
Ilia Ryabokon committed
591
    mValidator = new QRegExpValidator(QRegExp("[^\\/\\:\\?\\*\\|\\<\\>\\\"]{2,}"), this);
592 593 594 595
    mLineEdit->setValidator(mValidator);
    labelLayout->addWidget(mLabel);
    labelLayout->addWidget(mLineEdit);

596
    QHBoxLayout *buttonLayout = new QHBoxLayout();
597

598
    acceptButton = new QPushButton(acceptText, this);
599 600 601 602 603 604 605
    QPushButton *cancelButton = new QPushButton(cancelText, this);
    buttonLayout->addWidget(acceptButton);
    buttonLayout->addWidget(cancelButton);

    mainLayout->addLayout(labelLayout);
    mainLayout->addLayout(buttonLayout);

606 607
    acceptButton->setEnabled(false);

608 609
    connect(acceptButton, SIGNAL(clicked()), this, SLOT(accept()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
610 611 612
    connect(mLineEdit, SIGNAL(textEdited(QString)), this, SLOT(reactOnTextChanged(QString)));

    reactOnTextChanged(QString());
613 614 615 616 617 618
}

void UBFeaturesNewFolderDialog::setRegexp(const QRegExp pRegExp)
{
    mValidator->setRegExp(pRegExp);
}
619 620 621 622
bool UBFeaturesNewFolderDialog::validString(const QString &pStr)
{
    return mLineEdit->hasAcceptableInput() && !mFileNameList.contains(pStr, Qt::CaseSensitive);
}
623 624 625 626 627 628 629 630 631 632 633 634

void UBFeaturesNewFolderDialog::accept()
{
//     Setting all the constraints we need
    emit createNewFolder(mLineEdit->text());
    mLineEdit->clear();
}
void UBFeaturesNewFolderDialog::reject()
{
    mLineEdit->clear();
    emit closeDialog();
}
635 636 637 638 639 640 641 642 643 644 645 646 647 648
void UBFeaturesNewFolderDialog::setFileNameList(const QStringList &pLst)
{
    mFileNameList = pLst;
}
void UBFeaturesNewFolderDialog::reactOnTextChanged(const QString &pStr)
{
    if (validString(pStr)) {
        acceptButton->setEnabled(true);
        mLineEdit->setStyleSheet("background:white;");
    } else {
        acceptButton->setEnabled(false);
        mLineEdit->setStyleSheet("background:#FFB3C8;");
    }
}
649

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
UBFeaturesProgressInfo::UBFeaturesProgressInfo(QWidget *parent) :
    QWidget(parent),
    mProgressBar(0),
    mCommonInfoLabel(0),
    mDetailedInfoLabel(0)
{
    QVBoxLayout *mainLayer = new QVBoxLayout(this);

    mProgressBar = new QProgressBar(this);
//    setting defaults
    mProgressBar->setMinimum(0);
    mProgressBar->setMaximum(100000);
    mProgressBar->setValue(0);

    mProgressBar->setStyleSheet("background:white");

    mCommonInfoLabel = new QLabel(this);
    mDetailedInfoLabel = new QLabel(this);
    mDetailedInfoLabel->setAlignment(Qt::AlignRight);
    mCommonInfoLabel->hide();
    mDetailedInfoLabel->hide();

    mainLayer->addWidget(mCommonInfoLabel);
    mainLayer->addWidget(mDetailedInfoLabel);
    mainLayer->addWidget(mProgressBar);
}

void UBFeaturesProgressInfo::setCommmonInfoText(const QString &str)
{
679
    mProgressBar->setFormat(tr("Loading ") + str + " (%p%)");
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
}

void UBFeaturesProgressInfo::setDetailedInfoText(const QString &str)
{
    mDetailedInfoLabel->setText(str);
}

void UBFeaturesProgressInfo::setProgressMax(int pValue)
{
    mProgressBar->setMaximum(pValue);
}

void UBFeaturesProgressInfo::setProgressMin(int pValue)
{
    mProgressBar->setMinimum(pValue);
}

void UBFeaturesProgressInfo::increaseProgressValue()
{
    mProgressBar->setValue(mProgressBar->value() + 1);
}

void UBFeaturesProgressInfo::sendFeature(UBFeature pFeature)
{
    Q_UNUSED(pFeature);
}


708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
UBFeaturesWebView::UBFeaturesWebView(QWidget* parent, const char* name):QWidget(parent)
    , mpView(NULL)
    , mpWebSettings(NULL)
    , mpLayout(NULL)
    , mpSankoreAPI(NULL)
{
    setObjectName(name);

    SET_STYLE_SHEET();

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

    mpView = new QWebView(this);
    mpView->setObjectName("SearchEngineView");
    mpSankoreAPI = new UBWidgetUniboardAPI(UBApplication::boardController->activeScene());
    mpView->page()->mainFrame()->addToJavaScriptWindowObject("sankore", mpSankoreAPI);
Claudio Valerio's avatar
Claudio Valerio committed
725
    connect(mpView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(javaScriptWindowObjectCleared()));
726 727 728 729 730 731 732 733 734 735
    mpWebSettings = QWebSettings::globalSettings();
    mpWebSettings->setAttribute(QWebSettings::JavaEnabled, true);
    mpWebSettings->setAttribute(QWebSettings::PluginsEnabled, true);
    mpWebSettings->setAttribute(QWebSettings::LocalStorageDatabaseEnabled, true);
    mpWebSettings->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, true);
    mpWebSettings->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
    mpWebSettings->setAttribute(QWebSettings::JavascriptCanAccessClipboard, true);
    mpWebSettings->setAttribute(QWebSettings::DnsPrefetchEnabled, true);

    mpLayout->addWidget(mpView);
736
    mpLayout->setMargin(0);
737 738 739 740 741 742

    connect(mpView, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
}

UBFeaturesWebView::~UBFeaturesWebView()
{
743 744
    if( NULL != mpSankoreAPI )
    {
745 746 747
        delete mpSankoreAPI;
        mpSankoreAPI = NULL;
    }
748 749
    if( NULL != mpView )
    {
750 751 752
        delete mpView;
        mpView = NULL;
    }
753 754
    if( NULL != mpLayout )
    {
755 756 757 758 759
        delete mpLayout;
        mpLayout = NULL;
    }
}

Claudio Valerio's avatar
Claudio Valerio committed
760 761 762 763 764
void UBFeaturesWebView::javaScriptWindowObjectCleared()
{
    mpView->page()->mainFrame()->addToJavaScriptWindowObject("sankore", mpSankoreAPI);
}

765 766 767
void UBFeaturesWebView::showElement(const UBFeature &elem)
{
	QString qsWidgetName;
768
    QString path = elem.getFullPath().toLocalFile();
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806

	QString qsConfigPath = QString("%0/config.xml").arg(path);

	if(QFile::exists(qsConfigPath))
	{
		QFile f(qsConfigPath);
		if(f.open(QIODevice::ReadOnly))
		{
			QDomDocument domDoc;
			domDoc.setContent(QString(f.readAll()));
			QDomElement root = domDoc.documentElement();

			QDomNode node = root.firstChild();
			while(!node.isNull())
			{
				if(node.toElement().tagName() == "content")
				{
					QDomAttr srcAttr = node.toElement().attributeNode("src");
					qsWidgetName = srcAttr.value();
					break;
				}
				node = node.nextSibling();
			}
			f.close();
		}
	}

	mpView->load(QUrl::fromLocalFile(QString("%0/%1").arg(path).arg(qsWidgetName)));    
}

void UBFeaturesWebView::onLoadFinished(bool ok)
{
    if(ok && NULL != mpSankoreAPI){
        mpView->page()->mainFrame()->addToJavaScriptWindowObject("sankore", mpSankoreAPI);
    }
}


Anna Udovichenko's avatar
Anna Udovichenko committed
807 808 809 810 811 812 813
UBFeatureProperties::UBFeatureProperties( QWidget *parent, const char *name ) : QWidget(parent)
    , mpLayout(NULL)
    , mpButtonLayout(NULL)
    , mpAddPageButton(NULL)
    , mpAddToLibButton(NULL)
    , mpSetAsBackgroundButton(NULL)
    , mpObjInfoLabel(NULL)
814
    , mpObjInfos(NULL)
Anna Udovichenko's avatar
Anna Udovichenko committed
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
    , mpThumbnail(NULL)
    , mpOrigPixmap(NULL)
    , mpElement(NULL)
{
	setObjectName(name);

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

    maxThumbHeight = height() / 4;

    mpThumbnail = new QLabel();
    QPixmap icon(":images/libpalette/notFound.png");
    icon.scaledToWidth(THUMBNAIL_WIDTH);

    mpThumbnail->setPixmap(icon);
    mpThumbnail->setObjectName("DockPaletteWidgetBox");
    mpThumbnail->setStyleSheet("background:white;");
    mpThumbnail->setAlignment(Qt::AlignHCenter);
    mpLayout->addWidget(mpThumbnail, 0);

    mpButtonLayout = new QHBoxLayout();
    mpLayout->addLayout(mpButtonLayout, 0);

    mpAddPageButton = new UBFeatureItemButton();
    mpAddPageButton->setText(tr("Add to page"));
    mpButtonLayout->addWidget(mpAddPageButton);

    mpSetAsBackgroundButton = new UBFeatureItemButton();
    mpSetAsBackgroundButton->setText(tr("Set as background"));
    mpButtonLayout->addWidget(mpSetAsBackgroundButton);

    mpAddToLibButton = new UBFeatureItemButton();
    mpAddToLibButton->setText(tr("Add to library"));
    mpButtonLayout->addWidget(mpAddToLibButton);

    mpButtonLayout->addStretch(1);

854
	mpObjInfoLabel = new QLabel(tr("Object informations"));
Anna Udovichenko's avatar
Anna Udovichenko committed
855 856 857
    mpObjInfoLabel->setStyleSheet(QString("color: #888888; font-size : 18px; font-weight:bold;"));
    mpLayout->addWidget(mpObjInfoLabel, 0);

858 859 860 861 862 863 864 865
    mpObjInfos = new QTreeWidget(this);
    mpObjInfos->setColumnCount(2);
    mpObjInfos->header()->hide();
    mpObjInfos->setAlternatingRowColors(true);
    mpObjInfos->setRootIsDecorated(false);
    mpObjInfos->setObjectName("DockPaletteWidgetBox");
    mpObjInfos->setStyleSheet("background:white;");
    mpLayout->addWidget(mpObjInfos, 1);
866
    mpLayout->setMargin(0);
867

868 869
	connect( mpAddPageButton, SIGNAL(clicked()), this, SLOT(onAddToPage()) );
    connect( mpSetAsBackgroundButton, SIGNAL( clicked() ), this, SLOT( onSetAsBackground() ) );
870 871 872
    connect( mpAddToLibButton, SIGNAL( clicked() ), this, SLOT(onAddToLib() ) );
}

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
UBFeatureProperties::~UBFeatureProperties()
{
	if ( mpOrigPixmap )
    {
        delete mpOrigPixmap;
        mpOrigPixmap = NULL;
    }
	if ( mpElement )
	{
		delete mpElement;
		mpElement = NULL;
	}
    if ( mpThumbnail )
    {
        delete mpThumbnail;
        mpThumbnail = NULL;
    }
    if ( mpButtonLayout )
    {
        delete mpButtonLayout;
        mpButtonLayout = NULL;
    }
    if ( mpAddPageButton )
    {
        delete mpAddPageButton;
        mpAddPageButton = NULL;
    }
    if ( mpSetAsBackgroundButton )
    {
        delete mpSetAsBackgroundButton;
        mpSetAsBackgroundButton = NULL;
    }
    if ( mpAddToLibButton )
    {
        delete mpAddToLibButton;
        mpAddToLibButton = NULL;
    }
    if ( mpObjInfoLabel )
    {
        delete mpObjInfoLabel;
        mpObjInfoLabel = NULL;
    }
    if ( mpObjInfos )
    {
        delete mpObjInfos;
        mpObjInfos = NULL;
    }
}

922 923 924 925 926 927 928 929 930 931 932
void UBFeatureProperties::resizeEvent( QResizeEvent *event )
{
    Q_UNUSED(event);
    adaptSize();
}

void UBFeatureProperties::showEvent (QShowEvent *event )
{
    Q_UNUSED(event);
    adaptSize();
}
Anna Udovichenko's avatar
Anna Udovichenko committed
933

934 935 936 937
UBFeature UBFeatureProperties::getCurrentElement() const
{
    if ( mpElement )
        return *mpElement;
938

939 940 941
    return UBFeature();
}

942
void UBFeatureProperties::setOrigPixmap(const QPixmap &pix)
943 944 945 946 947 948 949 950
{
    
    if (mpOrigPixmap)
        delete mpOrigPixmap;

    mpOrigPixmap = new QPixmap(pix);
}

951
void UBFeatureProperties::setThumbnail(const QPixmap &pix)
952 953
{  
    mpThumbnail->setPixmap(pix.scaledToWidth(THUMBNAIL_WIDTH));
954
    adaptSize();
955 956
}

957 958 959 960 961 962 963 964 965 966 967 968 969
void UBFeatureProperties::adaptSize()
{
    if( NULL != mpOrigPixmap )
    {
        if( width() < THUMBNAIL_WIDTH + 40 )
        {
            mpThumbnail->setPixmap( mpOrigPixmap->scaledToWidth( width() - 40 ) );
        }
        else
        {
            mpThumbnail->setPixmap( mpOrigPixmap->scaledToWidth( THUMBNAIL_WIDTH ) );
        }
    }
Anna Udovichenko's avatar
Anna Udovichenko committed
970 971
}

972
void UBFeatureProperties::showElement(const UBFeature &elem)
Anna Udovichenko's avatar
Anna Udovichenko committed
973 974 975 976 977 978 979 980 981 982 983
{
	if ( mpOrigPixmap )
    {
        delete mpOrigPixmap;
        mpOrigPixmap = NULL;
    }
	if ( mpElement )
	{
		delete mpElement;
		mpElement = NULL;
	}
984 985 986
    mpElement = new UBFeature(elem);
    mpOrigPixmap = new QPixmap(QPixmap::fromImage(elem.getThumbnail()));
    mpThumbnail->setPixmap(QPixmap::fromImage(elem.getThumbnail()).scaledToWidth(THUMBNAIL_WIDTH));
987
    populateMetadata();
Anna Udovichenko's avatar
Anna Udovichenko committed
988

989
    if ( UBApplication::isFromWeb( elem.getFullPath().toString() ) )
Anna Udovichenko's avatar
Anna Udovichenko committed
990 991
	{
        mpAddToLibButton->show();
992
		if( elem.getMetadata()["Type"].toLower().contains("image") )
Anna Udovichenko's avatar
Anna Udovichenko committed
993 994 995 996 997 998
		{
            mpSetAsBackgroundButton->show();
        }
		else
		{
            mpSetAsBackgroundButton->hide();
999
        }
Anna Udovichenko's avatar
Anna Udovichenko committed
1000 1001 1002 1003
    }
	else
	{
        mpAddToLibButton->hide();
1004
        if (UBFileSystemUtils::mimeTypeFromFileName( elem.getFullPath().toLocalFile() ).contains("image"))
Anna Udovichenko's avatar
Anna Udovichenko committed
1005 1006 1007
		{
            mpSetAsBackgroundButton->show();
        }
1008
	    else
Anna Udovichenko's avatar
Anna Udovichenko committed
1009 1010 1011 1012 1013 1014
		{
            mpSetAsBackgroundButton->hide();
        }
    }
}

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
void UBFeatureProperties::populateMetadata()
{
    if(NULL != mpObjInfos){
        mpObjInfos->clear();
		QMap<QString, QString> metas = mpElement->getMetadata();
        QList<QString> lKeys = metas.keys();
        QList<QString> lValues = metas.values();

        for(int i=0; i< metas.size(); i++){
            QStringList values;
            values << lKeys.at(i);
            values << lValues.at(i);
            mpItem = new QTreeWidgetItem(values);
            mpObjInfos->addTopLevelItem(mpItem);
        }
        mpObjInfos->resizeColumnToContents(0);
    }
}

Anna Udovichenko's avatar
Anna Udovichenko committed
1034 1035
void UBFeatureProperties::onAddToPage()
{
1036 1037
    QWidget *w = parentWidget()->parentWidget()->parentWidget();
    UBFeaturesWidget* featuresWidget = qobject_cast<UBFeaturesWidget*>( w );
1038 1039
    if (featuresWidget)
        featuresWidget->getFeaturesController()->addItemToPage( *mpElement );
1040
}
Anna Udovichenko's avatar
Anna Udovichenko committed
1041

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
void UBFeatureProperties::onAddToLib()
{
    if ( UBApplication::isFromWeb(  mpElement->getFullPath().toString() ) )
    {
        sDownloadFileDesc desc;
        desc.isBackground = false;
        desc.modal = false;
        desc.name = QFileInfo( mpElement->getFullPath().toString()).fileName();
        qDebug() << desc.name;
        desc.url = mpElement->getFullPath().toString();
        qDebug() << desc.url;
        UBDownloadManager::downloadManager()->addFileToDownload(desc);
    }
}


1058 1059
void UBFeatureProperties::onSetAsBackground()
{
1060 1061
    QWidget *w = parentWidget()->parentWidget()->parentWidget();
    UBFeaturesWidget* featuresWidget = qobject_cast<UBFeaturesWidget*>( w );
1062
    featuresWidget->getFeaturesController()->addItemAsBackground( *mpElement );
Anna Udovichenko's avatar
Anna Udovichenko committed
1063 1064
}

1065

Anna Udovichenko's avatar
Anna Udovichenko committed
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

UBFeatureItemButton::UBFeatureItemButton(QWidget *parent, const char *name):QPushButton(parent)
{
    setObjectName(name);
    setStyleSheet(QString("background-color : #DDDDDD; color : #555555; border-radius : 6px; padding : 5px; font-weight : bold; font-size : 12px;"));
}

UBFeatureItemButton::~UBFeatureItemButton()
{
}

QVariant UBFeaturesModel::data(const QModelIndex &index, int role) const
{
	if (!index.isValid())
        return QVariant();

1082
    if (role == Qt::DisplayRole) {
Ilia Ryabokon's avatar
Ilia Ryabokon committed
1083
        return featuresList->at(index.row()).getDisplayName();
1084 1085 1086
    }

    else if (role == Qt::DecorationRole) {
1087
        return QIcon( QPixmap::fromImage(featuresList->at(index.row()).getThumbnail()));
1088 1089

    } else if (role == Qt::UserRole) {
1090
		return featuresList->at(index.row()).getVirtualPath();
1091 1092

    }	else if (role == Qt::UserRole + 1) {
Anna Udovichenko's avatar
Anna Udovichenko committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
		//return featuresList->at(index.row()).getType();
		UBFeature f = featuresList->at(index.row());
		return QVariant::fromValue( f );
	}

	return QVariant();
}

QMimeData* UBFeaturesModel::mimeData(const QModelIndexList &indexes) const
{
1103
    UBFeaturesMimeData *mimeData = new UBFeaturesMimeData();
Anna Udovichenko's avatar
Anna Udovichenko committed
1104
	QList <QUrl> urlList;
1105 1106
    QList <UBFeature> featuresList;
    QByteArray typeData;
Anna Udovichenko's avatar
Anna Udovichenko committed
1107

1108 1109 1110 1111
    foreach (QModelIndex index, indexes) {

        if (index.isValid()) {
            UBFeature element = data(index, Qt::UserRole + 1).value<UBFeature>();
1112
            urlList.push_back( element.getFullPath() );
1113 1114 1115 1116 1117 1118 1119
            QString curPath = element.getFullPath().toLocalFile();
            featuresList.append(element);

            if (!typeData.isNull()) {
                typeData += UBFeaturesController::featureTypeSplitter();
            }
            typeData += QString::number(element.getType()).toAscii();
Anna Udovichenko's avatar
Anna Udovichenko committed
1120 1121
		}
	}
1122 1123 1124 1125

    mimeData->setUrls(urlList);
    mimeData->setFeatures(featuresList);
    mimeData->setData(mimeSankoreFeatureTypes, typeData);
Anna Udovichenko's avatar
Anna Udovichenko committed
1126 1127 1128 1129 1130 1131

    return mimeData;
}

bool UBFeaturesModel::dropMimeData(const QMimeData *mimeData, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
Ivan Ilin's avatar
Ivan Ilin committed
1132 1133
    Q_UNUSED(row)

1134
    const UBFeaturesMimeData *fMimeData = qobject_cast<const UBFeaturesMimeData*>(mimeData);
1135 1136
    UBFeaturesController *curController = qobject_cast<UBFeaturesController *>(QObject::parent());

1137 1138 1139 1140 1141 1142
    bool dataFromSameModel = false;

    if (fMimeData)
        dataFromSameModel = true;

    if ((!mimeData->hasUrls() && !mimeData->hasImage()) )
Anna Udovichenko's avatar
Anna Udovichenko committed
1143 1144 1145 1146 1147 1148
		return false;
	if ( action == Qt::IgnoreAction )
		return true;
	if ( column > 0 )
		return false;

1149 1150
    UBFeature parentFeature;
    if (!parent.isValid()) {
1151
        parentFeature = curController->getCurrentElement();
1152 1153
    } else {
        parentFeature = parent.data( Qt::UserRole + 1).value<UBFeature>();
1154
    }
1155

1156
    if (dataFromSameModel) {
1157 1158 1159 1160 1161 1162 1163 1164
        QList<UBFeature> featList = fMimeData->features();
        for (int i = 0; i < featList.count(); i++) {
            UBFeature sourceElement;
            if (dataFromSameModel) {
                sourceElement = featList.at(i);
                moveData(sourceElement, parentFeature, Qt::MoveAction);
            }
        }
1165 1166 1167 1168 1169 1170 1171
    } else if (mimeData->hasUrls()) {
        QList<QUrl> urlList = mimeData->urls();
        foreach (QUrl curUrl, urlList) {
            qDebug() << "URl catched is " << curUrl.toLocalFile();
            curController->moveExternalData(curUrl, parentFeature);
        }
    } else if (mimeData->hasImage()) {
1172
        QImage image = qvariant_cast<QImage>( mimeData->imageData() );
1173 1174
        curController->importImage( image, parentFeature );

1175
    }
1176

Anna Udovichenko's avatar
Anna Udovichenko committed
1177 1178 1179
	return true;
}

1180 1181 1182
void UBFeaturesModel::addItem( const UBFeature &item )
{
	beginInsertRows( QModelIndex(), featuresList->size(), featuresList->size() );
Anna Udovichenko's avatar
Anna Udovichenko committed
1183
	featuresList->append( item );
1184 1185 1186
	endInsertRows();
}

1187 1188 1189 1190
void UBFeaturesModel::deleteFavoriteItem( const QString &path )
{
	for ( int i = 0; i < featuresList->size(); ++i )
	{
1191
        if ( !QString::compare( featuresList->at(i).getFullPath().toString(), path, Qt::CaseInsensitive ) &&
1192
			!QString::compare( featuresList->at(i).getVirtualPath(), "/root/favorites", Qt::CaseInsensitive ) )
1193 1194 1195 1196 1197 1198 1199
		{
			removeRow( i, QModelIndex() );
			return;
		}
	}
}

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
void UBFeaturesModel::deleteItem( const QString &path )
{
    for ( int i = 0; i < featuresList->size(); ++i )
	{
        if ( !QString::compare( featuresList->at(i).getFullPath().toString(), path, Qt::CaseInsensitive ) )
		{
			removeRow( i, QModelIndex() );
			return;
		}
	}
}

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
void UBFeaturesModel::deleteItem(const UBFeature &feature)
{
    int i = featuresList->indexOf(feature);
    if (i == -1) {
        qDebug() << "no matches in deleting item from UBFEaturesModel";
        return;
    }
    removeRow(i, QModelIndex());
}

Anna Udovichenko's avatar
Anna Udovichenko committed
1222 1223 1224 1225
bool UBFeaturesModel::removeRows( int row, int count, const QModelIndex & parent )
{
	if ( row < 0 )
		return false;
1226
	if ( row + count > featuresList->size() )
Anna Udovichenko's avatar
Anna Udovichenko committed
1227 1228
		return false;
	beginRemoveRows( parent, row, row + count - 1 );
1229 1230
	//featuresList->remove( row, count );
	featuresList->erase( featuresList->begin() + row, featuresList->begin() + row + count );
Anna Udovichenko's avatar
Anna Udovichenko committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	endRemoveRows();
	return true;
}

bool UBFeaturesModel::removeRow(  int row, const QModelIndex & parent )
{
	if ( row < 0 )
		return false;
	if ( row >= featuresList->size() )
		return false;
	beginRemoveRows( parent, row, row );
1242 1243
	//featuresList->remove( row );
	featuresList->erase( featuresList->begin() + row );
Anna Udovichenko's avatar
Anna Udovichenko committed
1244 1245 1246 1247
	endRemoveRows();
	return true;
}

1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
void UBFeaturesModel::moveData(const UBFeature &source, const UBFeature &destination
                               , Qt::DropAction action = Qt::CopyAction, bool deleteManualy)
{
    UBFeaturesController *curController = qobject_cast<UBFeaturesController *>(QObject::parent());
    if (!curController)
        return;

    QString sourcePath = source.getFullPath().toLocalFile();
    QString sourceVirtualPath = source.getVirtualPath();

    UBFeatureElementType sourceType = source.getType();
1259
    QImage sourceIcon = source.getThumbnail();
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299

    Q_ASSERT( QFileInfo( sourcePath ).exists() );

    QString name = QFileInfo( sourcePath ).fileName();
    QString destPath = destination.getFullPath().toLocalFile();

    QString destVirtualPath = destination.getFullVirtualPath();
    QString destFullPath = destPath + "/" + name;

    if ( sourcePath.compare(destFullPath, Qt::CaseInsensitive ) || destination.getType() != FEATURE_TRASH)
    {
        UBFileSystemUtils::copy(sourcePath, destFullPath);
        if (action == Qt::MoveAction) {
            curController->deleteItem( source.getFullPath() );
        }
    }

    //Passing all the source container ubdating dependancy pathes
    if (sourceType == FEATURE_FOLDER) {
        for (int i = 0; i < featuresList->count(); i++) {

            UBFeature &curFeature = (*featuresList)[i];

            QString curFeatureFullPath = curFeature.getFullPath().toLocalFile();
            QString curFeatureVirtualPath = curFeature.getVirtualPath();

            if (curFeatureFullPath.contains(sourcePath) && curFeatureFullPath != sourcePath) {

                UBFeature copyFeature = curFeature;
                QUrl newPath = QUrl::fromLocalFile(curFeatureFullPath.replace(sourcePath, destFullPath));
                QString newVirtualPath = curFeatureVirtualPath.replace(sourceVirtualPath, destVirtualPath);
                //when copying to trash don't change the real path
                if (destination.getType() != FEATURE_TRASH) {
                    // processing copy or move action for real FS
                    if (action == Qt::CopyAction) {
                        copyFeature.setFullPath(newPath);
                    } else {
                        curFeature.setFullPath(newPath);
                    }
                }
1300
                // processing copy or move action for virtual FS
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
                if (action == Qt::CopyAction) {
                    copyFeature.setFullVirtualPath(newVirtualPath);
                } else {
                    curFeature.setFullVirtualPath(newVirtualPath);
                }

                if (action == Qt::CopyAction) {
                    addItem(copyFeature);
                }
            }
        }
    }

Ilia Ryabokon's avatar
Ilia Ryabokon committed
1314
    UBFeature newElement( destVirtualPath + "/" + name, sourceIcon, name, QUrl::fromLocalFile(destFullPath), sourceType );
1315 1316 1317 1318 1319 1320 1321 1322
    addItem(newElement);

    if (deleteManualy) {
        deleteItem(source);
    }

    emit dataRestructured();
}
1323

Anna Udovichenko's avatar
Anna Udovichenko committed
1324 1325
Qt::ItemFlags UBFeaturesModel::flags( const QModelIndex &index ) const
{
1326
    Qt::ItemFlags resultFlags = QAbstractItemModel::flags(index);
Anna Udovichenko's avatar
Anna Udovichenko committed
1327 1328 1329
	if ( index.isValid() )
	{
		UBFeature item = index.data( Qt::UserRole + 1 ).value<UBFeature>();
1330
        if ( item.getType() == FEATURE_INTERACTIVE
1331 1332 1333 1334
             || item.getType() == FEATURE_ITEM
             || item.getType() == FEATURE_AUDIO
             || item.getType() == FEATURE_VIDEO
             || item.getType() == FEATURE_IMAGE
1335
             || item.getType() == FEATURE_FLASH
1336 1337
             || item.getType() == FEATURE_INTERNAL
             || item.getType() == FEATURE_FOLDER)
1338 1339 1340 1341 1342 1343 1344 1345

            resultFlags |= Qt::ItemIsDragEnabled;

        if ( item.isFolder() && !item.getVirtualPath().isNull() )
            resultFlags |= Qt::ItemIsDropEnabled;
    }

    return resultFlags;
Anna Udovichenko's avatar
Anna Udovichenko committed
1346 1347 1348 1349 1350 1351
}


QStringList UBFeaturesModel::mimeTypes() const
{
	QStringList types;
1352
    types << "text/uri-list" << "image/png" << "image/tiff" << "image/gif" << "image/jpeg";
Anna Udovichenko's avatar
Anna Udovichenko committed
1353 1354 1355 1356 1357
    return types;
}

int UBFeaturesModel::rowCount(const QModelIndex &parent) const
{
1358
    if (parent.isValid() || !featuresList)
Anna Udovichenko's avatar
Anna Udovichenko committed
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
        return 0;
    else
        return featuresList->size();
}

bool UBFeaturesProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex & sourceParent )const
{
	QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
	QString path = index.data( Qt::UserRole ).toString();
	
	return filterRegExp().exactMatch(path);
}

bool UBFeaturesSearchProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex & sourceParent )const
{
	QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
	/*QString name = sourceModel()->data(index, Qt::DisplayRole).toString();
	eUBLibElementType type = (eUBLibElementType)sourceModel()->data(index, Qt::UserRole + 1).toInt();*/

1378
    UBFeature feature = sourceModel()->data(index, Qt::UserRole + 1).value<UBFeature>();
1379
    bool isFile = feature.getType() == FEATURE_INTERACTIVE
1380 1381 1382 1383 1384
            || feature.getType() == FEATURE_INTERNAL
            || feature.getType() == FEATURE_ITEM
            || feature.getType() == FEATURE_AUDIO
            || feature.getType() == FEATURE_VIDEO
            || feature.getType() == FEATURE_IMAGE;
1385

1386 1387 1388
    return isFile
            && feature.getFullVirtualPath().contains(mFilterPrefix)
            && filterRegExp().exactMatch( feature.getName() );
Anna Udovichenko's avatar
Anna Udovichenko committed
1389 1390 1391 1392 1393
}

bool UBFeaturesPathProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex & sourceParent )const
{
	QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
1394
    UBFeature feature = sourceModel()->data(index, Qt::UserRole + 1).value<UBFeature>();
Anna Udovichenko's avatar
Anna Udovichenko committed
1395
	
1396
	return feature.isFolder() && path.startsWith( feature.getFullVirtualPath() );
Anna Udovichenko's avatar
Anna Udovichenko committed
1397 1398 1399 1400
}

QString	UBFeaturesItemDelegate::displayText ( const QVariant & value, const QLocale & locale ) const
{
Ivan Ilin's avatar
Ivan Ilin committed
1401 1402 1403
    Q_UNUSED(locale)

    QString text = value.toString();
Ilia Ryabokon's avatar
Ilia Ryabokon committed
1404
    text = text.replace(".wgt", "");
Anna Udovichenko's avatar
Anna Udovichenko committed
1405 1406 1407
	if (listView)
	{
		const QFontMetrics fm = listView->fontMetrics();
1408
        const QSize iSize = listView->gridSize();
Anna Udovichenko's avatar
Anna Udovichenko committed
1409
		return elidedText( fm, iSize.width(), Qt::ElideRight, text );
Anna Udovichenko's avatar
Anna Udovichenko committed
1410 1411 1412 1413
	}
	return text;
}

1414
UBFeaturesPathItemDelegate::UBFeaturesPathItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
Anna Udovichenko's avatar
Anna Udovichenko committed
1415 1416 1417 1418 1419 1420
{
	arrowPixmap = new QPixmap(":images/navig_arrow.png");
}

QString	UBFeaturesPathItemDelegate::displayText ( const QVariant & value, const QLocale & locale ) const
{
Ivan Ilin's avatar
Ivan Ilin committed
1421 1422 1423 1424
    Q_UNUSED(value)
    Q_UNUSED(locale)

    return QString();
Anna Udovichenko's avatar
Anna Udovichenko committed
1425 1426 1427 1428 1429 1430
}

void UBFeaturesPathItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
	UBFeature feature = index.data( Qt::UserRole + 1 ).value<UBFeature>();
	QRect rect = option.rect;
1431
	if ( !feature.getFullPath().isEmpty() )
Anna Udovichenko's avatar
Anna Udovichenko committed
1432
	{
Ilia Ryabokon's avatar
Ilia Ryabokon committed
1433
        painter->drawPixmap( rect.left() - 10, rect.center().y() - 5, *arrowPixmap );
Anna Udovichenko's avatar
Anna Udovichenko committed
1434
	}
1435
    painter->drawImage( rect.left() + 5, rect.center().y() - 5, feature.getThumbnail().scaledToHeight( 30, Qt::SmoothTransformation ) );
Anna Udovichenko's avatar
Anna Udovichenko committed
1436 1437 1438 1439 1440 1441 1442 1443 1444
}

UBFeaturesPathItemDelegate::~UBFeaturesPathItemDelegate()
{
	if ( arrowPixmap )
	{
		delete arrowPixmap;
		arrowPixmap = NULL;
	}
Ivan Ilin's avatar
Ivan Ilin committed
1445
}