comparison demos/browser/browsermainwindow.d @ 77:0654fc9bac95

more porting
author mandel
date Sun, 24 May 2009 13:46:32 +0000
parents 37caa90ce503
children 85c59c4e5f19
comparison
equal deleted inserted replaced
76:454e4b4beb59 77:0654fc9bac95
81 Handles the tab widget and all the actions 81 Handles the tab widget and all the actions
82 */ 82 */
83 class BrowserMainWindow : public QMainWindow 83 class BrowserMainWindow : public QMainWindow
84 { 84 {
85 85
86 static const qint32 BrowserMainWindowMagic = 0xba; 86 static const int BrowserMainWindowMagic = 0xba;
87 87
88 public: 88 public:
89 89
90 this(QWidget parent = null, Qt.WindowFlags flags = 0) 90 this(QWidget parent = null, Qt.WindowFlags flags = 0)
91 { 91 {
163 return size; 163 return size;
164 } 164 }
165 165
166 public: 166 public:
167 167
168 static QUrl guessUrlFromString(QString string) 168 static QUrl guessUrlFromString(string string)
169 { 169 {
170 QString urlStr = string.trimmed(); 170 string urlStr = string.trimmed();
171 auto test = new QRegExp(QLatin1String("^[a-zA-Z]+\\:.*")); 171 auto test = new QRegExp(QLatin1String("^[a-zA-Z]+\\:.*"));
172 172
173 // Check if it looks like a qualified URL. Try parsing it and see. 173 // Check if it looks like a qualified URL. Try parsing it and see.
174 bool hasSchema = test.exactMatch(urlStr); 174 bool hasSchema = test.exactMatch(urlStr);
175 if (hasSchema) { 175 if (hasSchema) {
186 186
187 // Might be a shorturl - try to detect the schema. 187 // Might be a shorturl - try to detect the schema.
188 if (!hasSchema) { 188 if (!hasSchema) {
189 int dotIndex = urlStr.indexOf(QLatin1Char('.')); 189 int dotIndex = urlStr.indexOf(QLatin1Char('.'));
190 if (dotIndex != -1) { 190 if (dotIndex != -1) {
191 QString prefix = urlStr.left(dotIndex).toLower(); 191 string prefix = urlStr.left(dotIndex).toLower();
192 QByteArray schema = (prefix == QLatin1String("ftp")) ? prefix.toLatin1() : "http"; 192 QByteArray schema = (prefix == QLatin1String("ftp")) ? prefix.toLatin1() : "http";
193 QUrl url = 193 QUrl url =
194 QUrl.fromEncoded(schema + "://" + urlStr.toUtf8(), QUrl.TolerantMode); 194 QUrl.fromEncoded(schema + "://" + urlStr.toUtf8(), QUrl.TolerantMode);
195 if (url.isValid()) 195 if (url.isValid())
196 return url; 196 return url;
220 { 220 {
221 int version_ = 2; 221 int version_ = 2;
222 QByteArray data; 222 QByteArray data;
223 auto stream = new QDataStream(&data, QIODevice.WriteOnly); 223 auto stream = new QDataStream(&data, QIODevice.WriteOnly);
224 224
225 stream << qint32(BrowserMainWindowMagic); 225 stream << cast(int) BrowserMainWindowMagic;
226 stream << qint32(version_); 226 stream << cast(int) version_;
227 227
228 stream << size(); 228 stream << size();
229 stream << !m_navigationBar.isHidden(); 229 stream << !m_navigationBar.isHidden();
230 stream << !m_bookmarksToolbar.isHidden(); 230 stream << !m_bookmarksToolbar.isHidden();
231 stream << !statusBar().isHidden(); 231 stream << !statusBar().isHidden();
232 if (withTabs) 232 if (withTabs)
233 stream << tabWidget().saveState(); 233 stream << tabWidget().saveState();
234 else 234 else
235 stream << QByteArray(); 235 stream << new QByteArray();
236 return data; 236 return data;
237 } 237 }
238 238
239 239
240 bool restoreState(QByteArray state) 240 bool restoreState(QByteArray state)
243 QByteArray sd = state; 243 QByteArray sd = state;
244 auto stream = new QDataStream(&sd, QIODevice.ReadOnly); 244 auto stream = new QDataStream(&sd, QIODevice.ReadOnly);
245 if (stream.atEnd()) 245 if (stream.atEnd())
246 return false; 246 return false;
247 247
248 qint32 marker; 248 int marker;
249 qint32 v; 249 int v;
250 stream >> marker; 250 stream >> marker;
251 stream >> v; 251 stream >> v;
252 if (marker != BrowserMainWindowMagic || v != version_) 252 if (marker != BrowserMainWindowMagic || v != version_)
253 return false; 253 return false;
254 254
281 return true; 281 return true;
282 } 282 }
283 283
284 public: 284 public:
285 285
286 void loadPage(QString page) 286 void loadPage(string page)
287 { 287 {
288 QUrl url = guessUrlFromString(page); 288 QUrl url = guessUrlFromString(page);
289 loadUrl(url); 289 loadUrl(url);
290 } 290 }
291 291
292 void slotHome() 292 void slotHome()
293 { 293 {
294 QSettings settings; 294 QSettings settings;
295 settings.beginGroup(QLatin1String("MainWindow")); 295 settings.beginGroup(QLatin1String("MainWindow"));
296 QString home = settings.value(QLatin1String("home"), QLatin1String("http://qtsoftware.com/")).toString(); 296 string home = settings.value(QLatin1String("home"), QLatin1String("http://qtsoftware.com/")).toString();
297 loadPage(home); 297 loadPage(home);
298 } 298 }
299 299
300 protected: 300 protected:
301 301
302 void closeEvent(QCloseEvent event) 302 void closeEvent(QCloseEvent event)
303 { 303 {
304 if (m_tabWidget.count() > 1) { 304 if (m_tabWidget.count() > 1) {
305 int ret = QMessageBox.warning(this, QString(), 305 int ret = QMessageBox.warning(this, null,
306 tr("Are you sure you want to close the window?" 306 tr("Are you sure you want to close the window?"
307 " There are %1 tab open").arg(m_tabWidget.count()), 307 " There are %1 tab open").arg(m_tabWidget.count()),
308 QMessageBox.Yes | QMessageBox.No, 308 QMessageBox.Yes | QMessageBox.No,
309 QMessageBox.No); 309 QMessageBox.No);
310 if (ret == QMessageBox.No) { 310 if (ret == QMessageBox.No) {
346 m_stopReload.triggered.connect(&m_reload.trigger); 346 m_stopReload.triggered.connect(&m_reload.trigger);
347 m_stopReload.setToolTip(tr("Reload the current page")); 347 m_stopReload.setToolTip(tr("Reload the current page"));
348 } 348 }
349 } 349 }
350 350
351 void slotUpdateStatusbar(QString string) 351 void slotUpdateStatusbar(string string)
352 { 352 {
353 statusBar().showMessage(string, 2000); 353 statusBar().showMessage(string, 2000);
354 } 354 }
355 355
356 void slotUpdateWindowTitle(QString title = QString()) 356 void slotUpdateWindowTitle(string title = null)
357 { 357 {
358 if (title.isEmpty()) { 358 if (title.isEmpty()) {
359 setWindowTitle(tr("Qt Demo Browser")); 359 setWindowTitle(tr("Qt Demo Browser"));
360 } else { 360 } else {
361 version(Q_WS_MAC) 361 version(Q_WS_MAC)
389 mw.slotHome(); 389 mw.slotHome();
390 } 390 }
391 391
392 void slotFileOpen() 392 void slotFileOpen()
393 { 393 {
394 QString file = QFileDialog.getOpenFileName(this, tr("Open Web Resource"), QString(), 394 string file = QFileDialog.getOpenFileName(this, tr("Open Web Resource"), null,
395 tr("Web Resources (*.html *.htm *.svg *.png *.gif *.svgz);;All files (*.*)")); 395 tr("Web Resources (*.html *.htm *.svg *.png *.gif *.svgz);;All files (*.*)"));
396 396
397 if (file.isEmpty()) 397 if (file.isEmpty())
398 return; 398 return;
399 399
422 void slotPrivateBrowsing() 422 void slotPrivateBrowsing()
423 { 423 {
424 QWebSettings settings = QWebSettings.globalSettings(); 424 QWebSettings settings = QWebSettings.globalSettings();
425 bool pb = settings.testAttribute(QWebSettings.PrivateBrowsingEnabled); 425 bool pb = settings.testAttribute(QWebSettings.PrivateBrowsingEnabled);
426 if (!pb) { 426 if (!pb) {
427 QString title = tr("Are you sure you want to turn on private browsing?"); 427 string title = tr("Are you sure you want to turn on private browsing?");
428 QString text = tr("<b>%1</b><br><br>When private browsing in turned on," 428 string text = tr("<b>%1</b><br><br>When private browsing in turned on,"
429 " webpages are not added to the history," 429 " webpages are not added to the history,"
430 " items are automatically removed from the Downloads window," 430 " items are automatically removed from the Downloads window,"
431 " new cookies are not stored, current cookies can't be accessed," 431 " new cookies are not stored, current cookies can't be accessed,"
432 " site icons wont be stored, session wont be saved, " 432 " site icons wont be stored, session wont be saved, "
433 " and searches are not addded to the pop-up menu in the Google search box." 433 " and searches are not addded to the pop-up menu in the Google search box."
434 " Until you close the window, you can still click the Back and Forward buttons" 434 " Until you close the window, you can still click the Back and Forward buttons"
435 " to return to the webpages you have opened.").arg(title); 435 " to return to the webpages you have opened.").arg(title);
436 436
437 QMessageBox.StandardButton button = QMessageBox.question(this, QString(), text, 437 QMessageBox.StandardButton button = QMessageBox.question(this, null, text,
438 QMessageBox.Ok | QMessageBox.Cancel, 438 QMessageBox.Ok | QMessageBox.Cancel,
439 QMessageBox.Ok); 439 QMessageBox.Ok);
440 if (button == QMessageBox.Ok) { 440 if (button == QMessageBox.Ok) {
441 settings.setAttribute(QWebSettings.PrivateBrowsingEnabled, true); 441 settings.setAttribute(QWebSettings.PrivateBrowsingEnabled, true);
442 } 442 }
443 } else { 443 } else {
444 settings.setAttribute(QWebSettings.PrivateBrowsingEnabled, false); 444 settings.setAttribute(QWebSettings.PrivateBrowsingEnabled, false);
445 445
446 BrowserMainWindow[] windows = BrowserApplication.instance().mainWindows(); 446 BrowserMainWindow[] windows = BrowserApplication.instance().mainWindows();
447 for (int i = 0; i < windows.count(); ++i) { 447 for (int i = 0; i < windows.length; ++i) {
448 BrowserMainWindow window = windows.at(i); 448 BrowserMainWindow window = windows[i];
449 window.m_lastSearch = null; //QString::null 449 window.m_lastSearch = null; //QString::null
450 window.tabWidget().clear(); 450 window.tabWidget().clear();
451 } 451 }
452 } 452 }
453 } 453 }
460 void slotEditFind() 460 void slotEditFind()
461 { 461 {
462 if (!currentTab()) 462 if (!currentTab())
463 return; 463 return;
464 bool ok; 464 bool ok;
465 QString search = QInputDialog.getText(this, tr("Find"), tr("Text:"), QLineEdit.Normal, m_lastSearch, &ok); 465 string search = QInputDialog.getText(this, tr("Find"), tr("Text:"), QLineEdit.Normal, m_lastSearch, &ok);
466 if (ok && !search.isEmpty()) { 466 if (ok && !search.isEmpty()) {
467 m_lastSearch = search; 467 m_lastSearch = search;
468 if (!currentTab().findText(m_lastSearch)) 468 if (!currentTab().findText(m_lastSearch))
469 slotUpdateStatusbar(tr("\"%1\" not found.").arg(m_lastSearch)); 469 slotUpdateStatusbar(tr("\"%1\" not found.").arg(m_lastSearch));
470 } 470 }
492 } 492 }
493 493
494 void slotAddBookmark() 494 void slotAddBookmark()
495 { 495 {
496 WebView webView = currentTab(); 496 WebView webView = currentTab();
497 QString url = webView.url().toString(); 497 string url = webView.url().toString();
498 QString title = webView.title(); 498 string title = webView.title();
499 AddBookmarkDialog dialog(url, title); 499 AddBookmarkDialog dialog(url, title);
500 dialog.exec(); 500 dialog.exec();
501 } 501 }
502 502
503 void slotViewZoomIn() 503 void slotViewZoomIn()
568 void slotViewPageSource() 568 void slotViewPageSource()
569 { 569 {
570 if (!currentTab()) 570 if (!currentTab())
571 return; 571 return;
572 572
573 QString markup = currentTab().page().mainFrame().toHtml(); 573 string markup = currentTab().page().mainFrame().toHtml();
574 QPlainTextEdit view = new QPlainTextEdit(markup); 574 QPlainTextEdit view = new QPlainTextEdit(markup);
575 view.setWindowTitle(tr("Page Source of %1").arg(currentTab().title())); 575 view.setWindowTitle(tr("Page Source of %1").arg(currentTab().title()));
576 view.setMinimumWidth(640); 576 view.setMinimumWidth(640);
577 view.setAttribute(Qt.WA_DeleteOnClose); 577 view.setAttribute(Qt.WA_DeleteOnClose);
578 view.show(); 578 view.show();
640 if (!currentTab()) 640 if (!currentTab())
641 return; 641 return;
642 QWebHistory history = currentTab().history(); 642 QWebHistory history = currentTab().history();
643 int historyCount = history.count(); 643 int historyCount = history.count();
644 for (int i = history.backItems(historyCount).count() - 1; i >= 0; --i) { 644 for (int i = history.backItems(historyCount).count() - 1; i >= 0; --i) {
645 QWebHistoryItem item = history.backItems(history.count()).at(i); 645 QWebHistoryItem item = history.backItems(history.count())[i];
646 QAction action = new QAction(this); 646 QAction action = new QAction(this);
647 action.setData(-1*(historyCount-i-1)); 647 action.setData(-1*(historyCount-i-1));
648 QIcon icon = BrowserApplication.instance().icon(item.url()); 648 QIcon icon = BrowserApplication.instance().icon(item.url());
649 action.setIcon(icon); 649 action.setIcon(icon);
650 action.setText(item.title()); 650 action.setText(item.title());
658 if (!currentTab()) 658 if (!currentTab())
659 return; 659 return;
660 QWebHistory history = currentTab().history(); 660 QWebHistory history = currentTab().history();
661 int historyCount = history.count(); 661 int historyCount = history.count();
662 for (int i = 0; i < history.forwardItems(history.count()).count(); ++i) { 662 for (int i = 0; i < history.forwardItems(history.count()).count(); ++i) {
663 QWebHistoryItem item = history.forwardItems(historyCount).at(i); 663 QWebHistoryItem item = history.forwardItems(historyCount)[i];
664 QAction action = new QAction(this); 664 QAction action = new QAction(this);
665 action.setData(historyCount-i); 665 action.setData(historyCount-i);
666 QIcon icon = BrowserApplication.instance().icon(item.url()); 666 QIcon icon = BrowserApplication.instance().icon(item.url());
667 action.setIcon(icon); 667 action.setIcon(icon);
668 action.setText(item.title()); 668 action.setText(item.title());
678 m_windowMenu.addSeparator(); 678 m_windowMenu.addSeparator();
679 m_windowMenu.addAction(tr("Downloads"), this, SLOT(slotDownloadManager()), QKeySequence(tr("Alt+Ctrl+L", "Download Manager"))); 679 m_windowMenu.addAction(tr("Downloads"), this, SLOT(slotDownloadManager()), QKeySequence(tr("Alt+Ctrl+L", "Download Manager")));
680 680
681 m_windowMenu.addSeparator(); 681 m_windowMenu.addSeparator();
682 BrowserMainWindow[] windows = BrowserApplication.instance().mainWindows(); 682 BrowserMainWindow[] windows = BrowserApplication.instance().mainWindows();
683 for (int i = 0; i < windows.count(); ++i) { 683 for (int i = 0; i < windows.length; ++i) {
684 BrowserMainWindow window = windows.at(i); 684 BrowserMainWindow window = windows[i];
685 QAction action = m_windowMenu.addAction(window.windowTitle(), this, SLOT(slotShowWindow())); 685 QAction action = m_windowMenu.addAction(window.windowTitle(), this, SLOT(slotShowWindow()));
686 action.setData(i); 686 action.setData(i);
687 action.setCheckable(true); 687 action.setCheckable(true);
688 if (window == this) 688 if (window == this)
689 action.setChecked(true); 689 action.setChecked(true);
700 history.goToItem(history.forwardItems(history.count() - offset + 1).back()); // forward 700 history.goToItem(history.forwardItems(history.count() - offset + 1).back()); // forward
701 } 701 }
702 702
703 void slotShowWindow() 703 void slotShowWindow()
704 { 704 {
705 if (QAction action = cast(QAction) sender()) { 705 if (QAction action = cast(QAction) signalSender()) {
706 QVariant v = action.data(); 706 QVariant v = action.data();
707 if (v.canConvert!(int)()) { 707 if (v.canConvert!(int)()) {
708 int offset = cast(int) v; 708 int offset = cast(int) v;
709 BrowserMainWindow[] windows = BrowserApplication.instance().mainWindows(); 709 BrowserMainWindow[] windows = BrowserApplication.instance().mainWindows();
710 windows.at(offset).activateWindow(); 710 windows[offset].activateWindow();
711 windows.at(offset).currentTab().setFocus(); 711 windows[offset].currentTab().setFocus();
712 } 712 }
713 } 713 }
714 } 714 }
715 715
716 void slotSwapFocus() 716 void slotSwapFocus()
1014 QAction m_addBookmark; 1014 QAction m_addBookmark;
1015 1015
1016 QIcon m_reloadIcon; 1016 QIcon m_reloadIcon;
1017 QIcon m_stopIcon; 1017 QIcon m_stopIcon;
1018 1018
1019 QString m_lastSearch; 1019 string m_lastSearch;
1020 } 1020 }