Skip to content

Commit

Permalink
Merge pull request #95 from felipealfonsog/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
felipealfonsog authored Apr 2, 2024
2 parents 053652e + 3a0f2f4 commit a73fdf6
Show file tree
Hide file tree
Showing 7 changed files with 1,693 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/cpp/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#############################################################################
# Makefile for building: novanav
# Generated by qmake (3.1) (Qt 5.15.12)
# Generated by qmake (3.1) (Qt 5.15.13)
# Project: novanav.pro
# Template: app
# Command: /usr/bin/qmake -o Makefile novanav.pro
Expand Down
187 changes: 131 additions & 56 deletions src/cpp/novanav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
#include <QtWebEngineWidgets/QWebEngineSettings>
#include <QtWebEngineWidgets/QWebEngineProfile>
#include <QTabBar>
#include <QtWebEngineWidgets/QWebEngineHistory> // Agregar esta línea
#include <QMenu>
#include <QtWebEngineWidgets/QWebEngineHistory> // Agregar esta línea
#include <QtWebEngineWidgets/QWebEngineContextMenuData> // Agregar esta línea
#include <QScreen>

class URLInputDialog : public QDialog
{
Expand Down Expand Up @@ -73,19 +75,30 @@ class NovaNav : public QMainWindow

connect(tab_widget, &QTabWidget::tabCloseRequested, this, &NovaNav::close_tab);

QShortcut *shortcut_quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
connect(shortcut_quit, &QShortcut::activated, qApp, &QApplication::quit);


// Set permissions and settings...
}

public slots:
void show_url_input_dialog()

{
if (url_input_dialog->exec() == QDialog::Accepted)
{

if (url_input_dialog->exec() == QDialog::Accepted)
QString url = url_input_dialog->url_entry->text();
// Verificar si la URL comienza con "http://" o "https://"
if (!url.startsWith("http://") && !url.startsWith("https://"))
{
QString url = url_input_dialog->url_entry->text();
create_new_tab(url);
// Si no comienza con "http://" o "https://", agregar "http://"
url = "http://" + url;
}
create_new_tab(url);
}
}


void close_tab(int index)
{
Expand Down Expand Up @@ -148,8 +161,6 @@ public slots:
// sqDebug() << "User-Agent:" << browser->page()->profile()->httpUserAgent();
// profile->setHttpUserAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36");



void create_new_tab(const QString &url)
{
// Crear un identificador único para el perfil
Expand All @@ -173,48 +184,13 @@ public slots:
connect(browser, &QWebEngineView::titleChanged, [=](const QString &title)
{ set_tab_title(browser, title.left(20)); });

// Conectar la señal de cambio de URL para abrir enlaces _blank en una nueva pestaña
connect(browser, &QWebEngineView::urlChanged, this, [=](const QUrl &newUrl)
{
// Verificar si el enlace es _blank
if (newUrl.toString().contains("_blank")) {
create_new_tab(newUrl.toString()); // Abrir enlace en nueva pestaña
} });

// Agregar el QWebEngineView a la pestaña
tab_widget->addTab(browser, "");
browser->setZoomFactor(0.67); // Zoom por defecto

// Configurar el menú contextual para abrir enlaces en nueva pestaña o ventana
browser->setContextMenuPolicy(Qt::CustomContextMenu);
connect(browser, &QWebEngineView::customContextMenuRequested, [=](const QPoint &pos)
{
// Obtener la URL del enlace bajo el cursor
QUrl linkUrl = browser->page()->url();
// Crear el menú contextual
QMenu contextMenu(tr("Context menu"), browser);
// Agregar acciones al menú
QAction *newTabAction = contextMenu.addAction(tr("Open link in new tab"));
QAction *newWindowAction = contextMenu.addAction(tr("Open link in new window"));
QAction *backAction = contextMenu.addAction(tr("Back"));
QAction *forwardAction = contextMenu.addAction(tr("Forward"));
QAction *refreshAction = contextMenu.addAction(tr("Refresh"));
// Mostrar el menú contextual y esperar a que se seleccione una acción
QAction *selectedAction = contextMenu.exec(browser->mapToGlobal(pos));
// Procesar la acción seleccionada
if (selectedAction == newTabAction) {
create_new_tab(linkUrl.toString()); // Abrir enlace en nueva pestaña
} else if (selectedAction == newWindowAction) {
QWebEngineView *newBrowser = new QWebEngineView();
newBrowser->setUrl(linkUrl); // Abrir enlace en nueva ventana
newBrowser->show();
} else if (selectedAction == backAction) {
browser->back(); // Retroceder en el historial
} else if (selectedAction == forwardAction) {
browser->forward(); // Avanzar en el historial
} else if (selectedAction == refreshAction) {
browser->reload(); // Actualizar página
} });
connect(browser, &QWebEngineView::customContextMenuRequested, this, &NovaNav::onCustomContextMenuRequested);
}

void set_tab_title(QWebEngineView *browser, const QString &title)
Expand Down Expand Up @@ -265,20 +241,10 @@ public slots:
tab_widget->setCornerWidget(navigation_container, Qt::TopRightCorner);
}

/*
void open_google_tab() {
QWebEngineView *current_browser = dynamic_cast<QWebEngineView *>(tab_widget->currentWidget());
if (current_browser) {
create_new_tab(current_browser->url().toString()); // Abrir la URL actual en una nueva pestaña
void open_google_tab()
{
create_new_tab("https://www.google.com");
}
}
*/

void open_google_tab() {
create_new_tab("https://www.google.com");
}



void show_credits_popup()
{
Expand All @@ -288,6 +254,7 @@ void open_google_tab() {
QVBoxLayout *credits_layout = new QVBoxLayout(credits_popup);

QLabel *credits_label = new QLabel(
"NovaNav - Super Lightweight Browser\n\n"
"Credits:\n"
"Computer Science Engineer: Felipe Alfonso González\n"
"GitHub: github.com/felipealfonsog\n"
Expand Down Expand Up @@ -334,6 +301,114 @@ void open_google_tab() {
current_browser->forward();
}
}


void open_new_window(const QUrl& url)
{
QWebEngineView *newBrowser = new QWebEngineView();
newBrowser->setUrl(url);
newBrowser->setZoomFactor(0.67); // Zoom al 67%
newBrowser->show();

// Conectar el menú contextual para el nuevo navegador
newBrowser->setContextMenuPolicy(Qt::CustomContextMenu);
connect(newBrowser, &QWebEngineView::customContextMenuRequested, this, &NovaNav::onCustomContextMenuRequestedForNewWindow);

// Centrar la ventana en la pantalla
QRect screenGeometry = QGuiApplication::primaryScreen()->geometry();
int x = (screenGeometry.width() - newBrowser->width()) / 2;
int y = (screenGeometry.height() - newBrowser->height()) / 2;
newBrowser->move(x, y);

// Establecer dimensiones de la ventana
newBrowser->resize(800, 500);
}

void onCustomContextMenuRequestedForNewWindow(const QPoint &pos)
{
QWebEngineView *newBrowser = dynamic_cast<QWebEngineView *>(sender());
if (!newBrowser)
return;

QMenu menu;

QAction *backAction = menu.addAction(tr("Back"));
QAction *forwardAction = menu.addAction(tr("Forward"));
QAction *refreshAction = menu.addAction(tr("Refresh"));
QAction *creditsAction = menu.addAction(tr("Credits"));

connect(backAction, &QAction::triggered, [=]() {
if (newBrowser->history()->canGoBack())
newBrowser->back();
});

connect(forwardAction, &QAction::triggered, [=]() {
if (newBrowser->history()->canGoForward())
newBrowser->forward();
});

connect(refreshAction, &QAction::triggered, [=]() {
newBrowser->reload();
});

connect(creditsAction, &QAction::triggered, this, &NovaNav::show_credits_popup);

menu.exec(newBrowser->mapToGlobal(pos));
}


void onCustomContextMenuRequested(const QPoint &pos)
{
QWebEngineView *browser = dynamic_cast<QWebEngineView *>(sender());
if (!browser)
return;

QMenu menu;

QAction *newTabAction = menu.addAction(tr("Open link in new tab"));
QAction *newWindowAction = menu.addAction(tr("Open link in new window"));
QAction *backAction = menu.addAction(tr("Back"));
QAction *forwardAction = menu.addAction(tr("Forward"));
QAction *refreshAction = menu.addAction(tr("Refresh"));
QAction *showTabsAction = menu.addAction(tr("Show/Hide Tabs"));
QAction *creditsAction = menu.addAction(tr("Credits"));

QUrl linkUrl = browser->page()->contextMenuData().linkUrl();
if (!linkUrl.isEmpty()) {
connect(newTabAction, &QAction::triggered, [=]() {
create_new_tab(linkUrl.toString());
});

connect(newWindowAction, &QAction::triggered, [=]() {
open_new_window(linkUrl);
});
} else {
newTabAction->setEnabled(false);
newWindowAction->setEnabled(false);
}

connect(backAction, &QAction::triggered, [=]() {
if (browser->history()->canGoBack())
browser->back();
});

connect(forwardAction, &QAction::triggered, [=]() {
if (browser->history()->canGoForward())
browser->forward();
});

connect(refreshAction, &QAction::triggered, [=]() {
browser->reload();
});

connect(showTabsAction, &QAction::triggered, this, &NovaNav::toggle_titles);

connect(creditsAction, &QAction::triggered, this, &NovaNav::show_credits_popup);

menu.exec(browser->mapToGlobal(pos));
}


};

int main(int argc, char *argv[])
Expand Down
File renamed without changes.
Loading

0 comments on commit a73fdf6

Please sign in to comment.