-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtabwidget.cpp
More file actions
264 lines (243 loc) · 7.66 KB
/
tabwidget.cpp
File metadata and controls
264 lines (243 loc) · 7.66 KB
1
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
28
29
30
31
32
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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Copyright 2013 KanMemo Project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "tabwidget.h"
#include "webpage.h"
#include "kanmusumemory_global.h"
#include <QSettings>
#include <QDebug>
class TabWidget::Private
{
public:
Private(TabWidget *parent);
void newTab(const QUrl &url, bool mobilemode = true);
void newTab(WebPage *webpage, bool mobilemode = true);
private:
TabWidget *q;
};
TabWidget::Private::Private(TabWidget *parent)
: q(parent)
{
}
//新しいタブ
void TabWidget::Private::newTab(const QUrl &url, bool mobilemode)
{
// qDebug() << "newTab:" << url << " : mobile=" << mobilemode;
WebPageForm *web = new WebPageForm(q);
web->setMobileMode(mobilemode);
web->setNetworkAccessManager(q->networkAccessManager());
web->setUrl(url);
q->addTab(web, QStringLiteral("(untitled)"));
q->setCurrentWidget(web);
//タブの追加
connect(web, &WebPageForm::addTabRequested, [this](WebPage *webpage){
newTab(webpage);
});
//タブを閉じる
connect(web, &WebPageForm::removeTabRequested, [this](WebPageForm *form) {
q->closeTab(form);
});
//お気に入りの更新
connect(web, &WebPageForm::updateFavorite, [this]() { emit q->updateFavorite(); });
}
void TabWidget::Private::newTab(WebPage *webpage, bool mobilemode)
{
// qDebug() << "newTab: webpage";
WebPageForm *web = new WebPageForm(q);
web->setMobileMode(mobilemode);
web->setWebPage(webpage);
web->setNetworkAccessManager(q->networkAccessManager());
q->addTab(web, QStringLiteral("(untitled)"));
q->setCurrentWidget(web);
//タブの追加
connect(web, &WebPageForm::addTabRequested, [this](WebPage *webpage){
newTab(webpage);
});
//タブを閉じる
connect(web, &WebPageForm::removeTabRequested, [this](WebPageForm *form) {
q->closeTab(form);
});
//お気に入りの更新
connect(web, &WebPageForm::updateFavorite, [this]() { emit q->updateFavorite(); });
}
TabWidget::TabWidget(QWidget* parent)
: QTabWidget(parent)
, d(new Private(this))
, m_saveOpenPage(true)
, m_openAndNewTab(true)
, m_cache(NULL)
, m_networkAccessManager(NULL)
{
connect(this, &QObject::destroyed, [this]() { delete d; });
//タブの閉じる要求
connect(this, &QTabWidget::tabCloseRequested, [this](int index) {
WebPageForm *form = reinterpret_cast<WebPageForm *>(widget(index));
// qDebug() << "close tab(" << index << "):" << form->url();
delete form;
});
}
//タブの状態を保存する
bool TabWidget::saveOpenPage() const
{
return m_saveOpenPage;
}
void TabWidget::setSaveOpenPage(bool saveOpenPage)
{
m_saveOpenPage = saveOpenPage;
}
//新しいURLを開くとき新しいタブ
bool TabWidget::openAndNewTab() const
{
return m_openAndNewTab;
}
void TabWidget::setOpenAndNewTab(bool openAndNewTab)
{
m_openAndNewTab = openAndNewTab;
}
//リンクを開く
void TabWidget::openUrl(const QUrl &url, bool mobilemode)
{
if(openAndNewTab() || count() == 0){
//設定が新規タブとタブが無いとき
newTab(url, mobilemode);
}else{
//現在のタブ
WebPageForm *form = reinterpret_cast<WebPageForm *>(widget(currentIndex()));
form->setMobileMode(mobilemode);
form->setUrl(url);
}
}
//新しいタブ
void TabWidget::newTab(const QUrl &url, bool mobilemode)
{
d->newTab(url, mobilemode);
}
//タブを閉じる
void TabWidget::closeTab()
{
tabCloseRequested(currentIndex());
}
//タブを閉じる
void TabWidget::closeTab(WebPageForm *form)
{
WebPageForm *f;
for(int i=0; i<count(); i++){
f = reinterpret_cast<WebPageForm *>(widget(i));
if(f == form){
tabCloseRequested(i);
}
}
}
//タブをすべて閉じる
void TabWidget::closeTabAll()
{
for(int i = count(); i >= 0; i++){
tabCloseRequested(i);
}
}
//タブの再読み込み
void TabWidget::reloadTab()
{
if(count() > 0){
WebPageForm *form = reinterpret_cast<WebPageForm *>(currentWidget());
form->reload();
}
}
//前のタブ
void TabWidget::prevTab()
{
int index = currentIndex() - 1;
if(index < 0){
index = count() - 1;
}
setCurrentIndex(index);
}
//次のタブ
void TabWidget::nextTab()
{
int index = currentIndex() + 1;
if(index >= count()){
index = 0;
}
setCurrentIndex(index);
}
void TabWidget::find()
{
WebPageForm *form = qobject_cast<WebPageForm *>(currentWidget());
if(form){
form->find();
}
}
//タブとかを読み込む
void TabWidget::load()
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope, KANMEMO_PROJECT, KANMEMO_NAME);
settings.beginGroup(QStringLiteral(SETTING_TAB));
setSaveOpenPage(settings.value(QStringLiteral(SETTING_TAB_SAVE_OPEN_PAGES), true).toBool());
setOpenAndNewTab(settings.value(QStringLiteral(SETTING_TAB_OPEN_AND_NEWTAB), true).toBool());
if(count() == 0 && saveOpenPage()){ //タブが無いときだけ復元
QList<QVariant> pagelist = settings.value(QStringLiteral(SETTING_TAB_OPEN_PAGES), QVariant()).toList();
QList<QVariant> pageMobileModeList = settings.value(QStringLiteral(SETTING_TAB_OPEN_PAGES_MOBILE_MODE), QVariant()).toList();
int i=0;
foreach (QVariant page, pagelist) {
if(i < pageMobileModeList.count()){
newTab(page.toString(), pageMobileModeList[i].toBool());
i++;
}else{
newTab(page.toString());
}
}
}
settings.endGroup();
}
//タブとか設定を保存する
void TabWidget::save()
{
QList<QVariant> pagelist;
QList<QVariant> pageMobileModeList;
WebPageForm *form;
QSettings settings(QSettings::IniFormat, QSettings::UserScope, KANMEMO_PROJECT, KANMEMO_NAME);
settings.beginGroup(QStringLiteral(SETTING_TAB));
settings.setValue(QStringLiteral(SETTING_TAB_SAVE_OPEN_PAGES), saveOpenPage());
settings.setValue(QStringLiteral(SETTING_TAB_OPEN_AND_NEWTAB), openAndNewTab());
if(saveOpenPage()){
for(int i=0; i<count(); i++){
form = reinterpret_cast<WebPageForm *>(widget(i));
pagelist.append(form->url().toString());
pageMobileModeList.append(form->isMobileMode());
}
}
settings.setValue(QStringLiteral(SETTING_TAB_OPEN_PAGES), pagelist);
settings.setValue(QStringLiteral(SETTING_TAB_OPEN_PAGES_MOBILE_MODE), pageMobileModeList);
settings.endGroup();
}
//キャッシュをタブの中のWebページへ設定するための情報
QNetworkDiskCache *TabWidget::cache() const
{
return m_cache;
}
void TabWidget::setCache(QNetworkDiskCache *cache)
{
m_cache = cache;
}
//
QNetworkAccessManager *TabWidget::networkAccessManager() const
{
return m_networkAccessManager;
}
void TabWidget::setNetworkAccessManager(QNetworkAccessManager *networkAccessManager)
{
m_networkAccessManager = networkAccessManager;
}