1 /*
2 * maintoolbar.cpp
3 * Copyright 2016, Thorbjørn Lindeijer <bjorn@lindijer.nl>
4 *
5 * This file is part of Tiled.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "maintoolbar.h"
22
23 #include "actionmanager.h"
24 #include "commandbutton.h"
25 #include "documentmanager.h"
26 #include "utils.h"
27
28 #include <QAction>
29 #include <QEvent>
30 #include <QMenu>
31 #include <QToolButton>
32 #include <QUndoGroup>
33
34 namespace Tiled {
35
MainToolBar(QWidget * parent)36 MainToolBar::MainToolBar(QWidget *parent)
37 : QToolBar(parent)
38 , mCommandButton(new CommandButton(this))
39 {
40 setObjectName(QLatin1String("MainToolBar"));
41 setWindowTitle(tr("Main Toolbar"));
42 setToolButtonStyle(Qt::ToolButtonFollowStyle);
43
44 QIcon newIcon(QLatin1String(":images/24/document-new.png"));
45 newIcon.addFile(QLatin1String(":images/16/document-new.png"));
46
47 mNewButton = new QToolButton(this);
48
49 QMenu *newMenu = new QMenu(this);
50 newMenu->addAction(ActionManager::action("NewMap"));
51 newMenu->addAction(ActionManager::action("NewTileset"));
52 mNewButton->setMenu(newMenu);
53 mNewButton->setPopupMode(QToolButton::InstantPopup);
54 mNewButton->setIcon(newIcon);
55
56 Utils::setThemeIcon(mNewButton, "document-new");
57
58 addWidget(mNewButton);
59 addAction(ActionManager::action("Open"));
60 addAction(ActionManager::action("Save"));
61 addSeparator();
62 addAction(ActionManager::action("Undo"));
63 addAction(ActionManager::action("Redo"));
64 addSeparator();
65 addWidget(mCommandButton);
66
67 DocumentManager *documentManager = DocumentManager::instance();
68
69 connect(documentManager, &DocumentManager::currentDocumentChanged,
70 this, &MainToolBar::currentDocumentChanged);
71
72 connect(this, &MainToolBar::orientationChanged,
73 this, &MainToolBar::onOrientationChanged);
74
75 retranslateUi();
76 }
77
changeEvent(QEvent * event)78 void MainToolBar::changeEvent(QEvent *event)
79 {
80 QToolBar::changeEvent(event);
81 switch (event->type()) {
82 case QEvent::LanguageChange:
83 retranslateUi();
84 break;
85 default:
86 break;
87 }
88 }
89
onOrientationChanged(Qt::Orientation orientation)90 void MainToolBar::onOrientationChanged(Qt::Orientation orientation)
91 {
92 setToolButtonStyle(orientation == Qt::Horizontal ? Qt::ToolButtonFollowStyle :
93 Qt::ToolButtonIconOnly);
94 }
95
currentDocumentChanged(Document * document)96 void MainToolBar::currentDocumentChanged(Document *document)
97 {
98 mCommandButton->setEnabled(document && !document->fileName().isEmpty());
99 }
100
retranslateUi()101 void MainToolBar::retranslateUi()
102 {
103 mNewButton->setToolTip(tr("New"));
104 }
105
106 } // namespace Tiled
107
108 #include "moc_maintoolbar.cpp"
109