UBLibItemProperties.cpp 8.7 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
15 16

#include "UBLibWidget.h"
Claudio Valerio's avatar
Claudio Valerio committed
17 18 19
#include "UBLibItemProperties.h"

#include "core/UBApplication.h"
20
#include "core/UBDownloadManager.h"
Claudio Valerio's avatar
Claudio Valerio committed
21 22 23

#include "frameworks/UBFileSystemUtils.h"

24 25
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/**
 * \brief Constructor
 * @param parent as the parent widget
 * @param name as the object name
 */
UBLibItemProperties::UBLibItemProperties(QWidget *parent, const char *name):QWidget(parent)
    , mpLayout(NULL)
    , mpButtonLayout(NULL)
    , mpAddPageButton(NULL)
    , mpAddToLibButton(NULL)
    , mpSetAsBackgroundButton(NULL)
    , mpObjInfoLabel(NULL)
    , mpObjInfos(NULL)
    , mpThumbnail(NULL)
    , mpOrigPixmap(NULL)
    , mpElement(NULL)
42
    , mpItem(NULL)
Claudio Valerio's avatar
Claudio Valerio committed
43 44 45
{
    setObjectName(name);

shibakaneki's avatar
shibakaneki committed
46 47 48
    setAttribute(Qt::WA_StyledBackground, true);
    setStyleSheet(UBApplication::globalStyleSheet());

Claudio Valerio's avatar
Claudio Valerio committed
49 50 51 52 53 54 55 56 57 58 59
    // 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);
shibakaneki's avatar
shibakaneki committed
60 61
    mpThumbnail->setObjectName("DockPaletteWidgetBox");
    mpThumbnail->setStyleSheet("background:white;");
Claudio Valerio's avatar
Claudio Valerio committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    mpThumbnail->setAlignment(Qt::AlignHCenter);
    mpLayout->addWidget(mpThumbnail, 0);

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

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

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

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

    mpButtonLayout->addStretch(1);

    mpObjInfoLabel = new QLabel(tr("Object informations"));
    mpObjInfoLabel->setStyleSheet(QString("color: #888888; font-size : 18px; font-weight:bold;"));
    mpLayout->addWidget(mpObjInfoLabel, 0);

86 87 88 89 90
    mpObjInfos = new QTreeWidget(this);
    mpObjInfos->setColumnCount(2);
    mpObjInfos->header()->hide();
    mpObjInfos->setAlternatingRowColors(true);
    mpObjInfos->setRootIsDecorated(false);
shibakaneki's avatar
shibakaneki committed
91 92
    mpObjInfos->setObjectName("DockPaletteWidgetBox");
    mpObjInfos->setStyleSheet("background:white;");
Claudio Valerio's avatar
Claudio Valerio committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    mpLayout->addWidget(mpObjInfos, 1);

    connect(mpAddPageButton, SIGNAL(clicked()), this, SLOT(onAddToPage()));
    connect(mpSetAsBackgroundButton, SIGNAL(clicked()), this, SLOT(onSetAsBackground()));
    connect(mpAddToLibButton, SIGNAL(clicked()), this, SLOT(onAddToLib()));

}

/**
 * \brief Destructor
 */
UBLibItemProperties::~UBLibItemProperties()
{
    if(NULL != mpOrigPixmap)
    {
        delete mpOrigPixmap;
        mpOrigPixmap = NULL;
    }
    if(NULL != mpLayout)
    {
        delete mpLayout;
        mpLayout = NULL;
    }
    if(NULL != mpSetAsBackgroundButton)
    {
        delete mpSetAsBackgroundButton;
        mpSetAsBackgroundButton = NULL;
    }
    if(NULL != mpAddPageButton)
    {
        delete mpAddPageButton;
        mpAddPageButton = NULL;
    }
    if(NULL != mpAddToLibButton)
    {
        delete mpAddToLibButton;
        mpAddToLibButton = NULL;
    }
    if(NULL != mpObjInfoLabel)
    {
        delete mpObjInfoLabel;
        mpObjInfoLabel = NULL;
    }
    if(NULL != mpObjInfos)
    {
        delete mpObjInfos;
        mpObjInfos = NULL;
    }
    if(NULL != mpThumbnail)
    {
        delete mpThumbnail;
        mpThumbnail = NULL;
    }
}

/**
 * \brief Handle the resize event
 * @param event as the resize event
 */
void UBLibItemProperties::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    adaptSize();
}

/**
 * \brief Adapt the thumbnail size
 */
void UBLibItemProperties::adaptSize()
{
    if(NULL != mpOrigPixmap)
    {
        if(width() < THUMBNAIL_WIDTH + 40)
        {
            mpThumbnail->setPixmap(mpOrigPixmap->scaledToWidth(width() - 40));
        }
        else
        {
            mpThumbnail->setPixmap(mpOrigPixmap->scaledToWidth(THUMBNAIL_WIDTH));
        }
    }
}

/**
 * \brief Add the element to the page
 */
void UBLibItemProperties::onAddToPage()
{
181 182 183 184 185 186 187 188 189 190 191 192
    if(UBApplication::isFromWeb(mpElement->path().toString())){
        sDownloadFileDesc desc;
        desc.isBackground = false;
        desc.modal = true;
        desc.name = QFileInfo(mpElement->path().toString()).fileName();
        desc.url = mpElement->path().toString();
        UBDownloadManager::downloadManager()->addFileToDownload(desc);

    }else{
        UBLibWidget* libWidget = dynamic_cast<UBLibWidget*>(parentWidget()->parentWidget());
        libWidget->libNavigator()->libraryWidget()->libraryController()->addItemToPage(mpElement);
    }
Claudio Valerio's avatar
Claudio Valerio committed
193 194 195 196 197 198 199
}

/**
 * \brief Add the item to the library
 */
void UBLibItemProperties::onAddToLib()
{
200 201 202 203 204 205 206 207
    if(UBApplication::isFromWeb(mpElement->path().toString())){
        sDownloadFileDesc desc;
        desc.isBackground = false;
        desc.modal = false;
        desc.name = QFileInfo(mpElement->path().toString()).fileName();
        desc.url = mpElement->path().toString();
        UBDownloadManager::downloadManager()->addFileToDownload(desc);
    }
Claudio Valerio's avatar
Claudio Valerio committed
208 209 210 211 212 213 214
}

/**
 * \brief Set the item as background
 */
void UBLibItemProperties::onSetAsBackground()
{
215 216 217 218 219 220 221 222 223 224 225 226
    if(UBApplication::isFromWeb(mpElement->path().toString())){
        sDownloadFileDesc desc;
        desc.isBackground = true;
        desc.modal = true;
        desc.name = QFileInfo(mpElement->path().toString()).fileName();
        desc.url = mpElement->path().toString();
        UBDownloadManager::downloadManager()->addFileToDownload(desc);

    }else{
        UBLibWidget* libWidget = dynamic_cast<UBLibWidget*>(parentWidget()->parentWidget());
        libWidget->libNavigator()->libraryWidget()->libraryController()->setItemAsBackground(mpElement);
    }
Claudio Valerio's avatar
Claudio Valerio committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
}

/**
 * \brief Show the given element properties
 * @param elem as the given element
 */
void UBLibItemProperties::showElement(UBLibElement *elem)
{
    if(NULL != mpOrigPixmap)
    {
        delete mpOrigPixmap;
        mpOrigPixmap = NULL;
    }
    if(NULL != elem)
    {
        mpElement = elem;
        mpOrigPixmap = new QPixmap(QPixmap::fromImage(*elem->thumbnail()));
        mpThumbnail->setPixmap(QPixmap::fromImage(*elem->thumbnail()).scaledToWidth(THUMBNAIL_WIDTH));
245
        populateMetadata();
Claudio Valerio's avatar
Claudio Valerio committed
246 247
    }

248 249 250 251 252 253 254 255
    if(UBApplication::isFromWeb(elem->path().toString())){
        mpAddToLibButton->show();
        if(elem->metadatas()["Type"].toLower().contains("image")){
            mpSetAsBackgroundButton->show();
        }else{
            mpSetAsBackgroundButton->hide();
        }
    }else{
Claudio Valerio's avatar
Claudio Valerio committed
256
        mpAddToLibButton->hide();
257 258 259 260 261
        if(UBFileSystemUtils::mimeTypeFromFileName(elem->path().toLocalFile()).contains("image")){
            mpSetAsBackgroundButton->show();
        }else{
            mpSetAsBackgroundButton->hide();
        }
Claudio Valerio's avatar
Claudio Valerio committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    }
}

/**
 * \brief Navigate backward
 */
void UBLibItemProperties::onBack()
{
    emit showFolderContent();
}

/**
 * \brief Handle the show event
 * @param event as the show event
 */
void UBLibItemProperties::showEvent(QShowEvent *event)
{
    Q_UNUSED(event);
    adaptSize();
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
void UBLibItemProperties::populateMetadata()
{
    if(NULL != mpObjInfos){
        mpObjInfos->clear();
        QMap<QString, QString> metas = mpElement->metadatas();
        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);
    }
}

Claudio Valerio's avatar
Claudio Valerio committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
/**
 * \brief Constructor
 * @param parent as the parent widget
 * @param name as the object name
 */
UBLibItemButton::UBLibItemButton(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;"));
}

/**
 * \brief Destructor
 */
UBLibItemButton::~UBLibItemButton()
{

}