1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /*******************************************************************************
21  *  Includes
22  ******************************************************************************/
23 #include "librarylistwidgetitem.h"
24 
25 #include "ui_librarylistwidgetitem.h"
26 
27 #include <librepcb/workspace/workspace.h>
28 
29 #include <QtCore>
30 #include <QtWidgets>
31 
32 /*******************************************************************************
33  *  Namespace
34  ******************************************************************************/
35 namespace librepcb {
36 namespace library {
37 namespace manager {
38 
39 /*******************************************************************************
40  *  Constructors / Destructor
41  ******************************************************************************/
42 
LibraryListWidgetItem(workspace::Workspace & ws,const FilePath & libDir,const QString & name,const QString & description,const QPixmap & icon)43 LibraryListWidgetItem::LibraryListWidgetItem(workspace::Workspace& ws,
44                                              const FilePath& libDir,
45                                              const QString& name,
46                                              const QString& description,
47                                              const QPixmap& icon) noexcept
48   : QWidget(nullptr),
49     mUi(new Ui::LibraryListWidgetItem),
50     mLibDir(libDir),
51     mIsRemoteLibrary(libDir.isLocatedInDir(ws.getRemoteLibrariesPath())) {
52   mUi->setupUi(this);
53 
54   if (mLibDir.isValid()) {
55     if (!icon.isNull()) {
56       mUi->lblIcon->setPixmap(icon);
57     }
58     mUi->lblLibraryName->setText(name);
59     mUi->lblLibraryDescription->setText(description);
60     QString path = libDir.toRelative(ws.getLibrariesPath());
61     path.replace("local/", "<font color=\"blue\">local</font>/");
62     path.replace("remote/", "<font color=\"red\">remote</font>/");
63     mUi->lblLibraryUrl->setText(path);
64   } else {
65     QPixmap image(":/img/actions/add.png");
66     mUi->lblIcon->setPixmap(image.scaled(
67         mUi->lblIcon->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
68     mUi->lblLibraryName->setText(tr("Add a new library"));
69     mUi->lblLibraryDescription->setText(tr("Click here to add a new library."));
70     mUi->lblLibraryUrl->setText("");
71   }
72 
73   if (mUi->lblLibraryDescription->text().isEmpty()) {
74     mUi->lblLibraryDescription->setVisible(false);
75   }
76 }
77 
~LibraryListWidgetItem()78 LibraryListWidgetItem::~LibraryListWidgetItem() noexcept {
79 }
80 
81 /*******************************************************************************
82  *  Getters
83  ******************************************************************************/
84 
getName() const85 QString LibraryListWidgetItem::getName() const noexcept {
86   return mUi->lblLibraryName->text();
87 }
88 
89 /*******************************************************************************
90  *  Inherited from QWidget
91  ******************************************************************************/
92 
mouseDoubleClickEvent(QMouseEvent * e)93 void LibraryListWidgetItem::mouseDoubleClickEvent(QMouseEvent* e) noexcept {
94   if (mLibDir.isValid()) {
95     emit openLibraryEditorTriggered(mLibDir);
96     e->accept();
97   } else {
98     QWidget::mouseDoubleClickEvent(e);
99   }
100 }
101 
102 /*******************************************************************************
103  *  End of File
104  ******************************************************************************/
105 
106 }  // namespace manager
107 }  // namespace library
108 }  // namespace librepcb
109