<commit_before>/*
This file is part of qNotesManager.

qNotesManager is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

qNotesManager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with qNotesManager. If not, see <http://www.gnu.org/licenses/>.
*/

#include "notestabwidget.h"

#include "noteeditwidget.h"
#include "note.h"
#include "global.h"

#include <QBoxLayout>
#include <QApplication>

using namespace qNotesManager;

NotesTabWidget::NotesTabWidget(QWidget *parent) : QWidget(parent) {
	tabWidget = new QTabWidget(this);
	QObject::connect(tabWidget, SIGNAL(currentChanged(int)),
					 this, SLOT(sl_TabWidget_CurrentChanged(int)));

#if QT_VERSION < 0x040500
	closeTabButton = new QPushButton();
	closeTabButton->setToolTip("Close tab");
	closeTabButton->setIcon(QIcon(":/gui/cross-small"));
	closeTabButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
	QObject::connect(closeTabButton, SIGNAL(clicked()),
					 this, SLOT(sl_CloseWidgetButton_Clicked()));
	tabWidget->setCornerWidget(closeTabButton);
#else
	tabWidget->setTabsClosable(true);
	QObject::connect(tabWidget, SIGNAL(tabCloseRequested(int)),
					 this, SLOT(sl_TabWidget_TabCloseRequested(int)));
#endif


	layout = new QBoxLayout(QBoxLayout::LeftToRight);
#if QT_VERSION < 0x040300
	layout->setMargin(0);
#else
	layout->setContentsMargins(0, 0, 0, 0);
#endif

	layout->addWidget(tabWidget);

	setLayout(layout);
}

void NotesTabWidget::OpenNote(Note* note, int position, bool newTab) {
	(void)newTab;

	if (!note) {
		WARNING("Null pointer recieved");
		return;
	}

	if (hash.contains(note)) {
		QWidget* noteWidget = hash.value(note);
		tabWidget->setCurrentWidget(noteWidget);
		return;
	}

	bool showWaitCursor = !note->TextDocumentInitialized();
	if (showWaitCursor) {
		QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
	}
	NoteEditWidget* w = new NoteEditWidget(note);
	if (showWaitCursor) {
		QApplication::restoreOverrideCursor();
	}
	hash.insert(note, w);
	QObject::connect(note, SIGNAL(sg_VisualPropertiesChanged()),
					 this, SLOT(sl_Note_PropertiesChanged()));


	tabWidget->addTab(w, note->GetIcon(), cropStringForTabCaption(note->GetName()));
	tabWidget->setCurrentWidget(w);
	w->ScrollTo(position);
}

void NotesTabWidget::CloseNote(const Note* n) {
	if (!n) {
		WARNING("Null pointer recieved");
		return;
	}

	if (!hash.contains(n)) {
		WARNING("Specified note is not open");
		return;
	}

	QWidget* widget = hash.take(n);
	int tabIndex = tabWidget->indexOf(widget);

	if (tabIndex == -1) {
		WARNING("Could not find associated widget");
		return;
	}

	QObject::disconnect(n, 0, this, 0);
	tabWidget->removeTab(tabIndex);
	delete widget;
}

void NotesTabWidget::ShowFragment(const NoteFragment& fragment) {
	if (!fragment.NotePrt) {
		WARNING("Null pointer recieved");
		return;
	}

	if (!hash.contains(fragment.NotePrt)) {
		OpenNote(const_cast<Note*>(fragment.NotePrt));
	}
	NoteEditWidget* noteWidget =
			dynamic_cast<NoteEditWidget*>(hash.value(fragment.NotePrt));
	noteWidget->ShowFragment(fragment);
}

void NotesTabWidget::Clear() {
	while (hash.count() > 0) {
		CloseNote(hash.keys().at(0));
	}
}

void NotesTabWidget::sl_Note_PropertiesChanged() {
	const Note* note = qobject_cast<Note*>(QObject::sender());
	if (note == 0) {
		WARNING("Could not find sender note");
		return;
	}

	if (!hash.contains(note)) {
		WARNING("Sender note is not registered");
		QObject::disconnect(note, 0, this, 0);
		return;
	}

	QWidget* widget = hash.value(note);
	int tabIndex = tabWidget->indexOf(widget);<commit_msg>Fixed bug when double click on search result did not activate note's tab properly. Fixes #3<commit_after>/*
This file is part of qNotesManager.

qNotesManager is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

qNotesManager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with qNotesManager. If not, see <http://www.gnu.org/licenses/>.
*/

#include "notestabwidget.h"

#include "noteeditwidget.h"
#include "note.h"
#include "global.h"

#include <QBoxLayout>
#include <QApplication>

using namespace qNotesManager;

NotesTabWidget::NotesTabWidget(QWidget *parent) : QWidget(parent) {
	tabWidget = new QTabWidget(this);
	QObject::connect(tabWidget, SIGNAL(currentChanged(int)),
					 this, SLOT(sl_TabWidget_CurrentChanged(int)));

#if QT_VERSION < 0x040500
	closeTabButton = new QPushButton();
	closeTabButton->setToolTip("Close tab");
	closeTabButton->setIcon(QIcon(":/gui/cross-small"));
	closeTabButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
	QObject::connect(closeTabButton, SIGNAL(clicked()),
					 this, SLOT(sl_CloseWidgetButton_Clicked()));
	tabWidget->setCornerWidget(closeTabButton);
#else
	tabWidget->setTabsClosable(true);
	QObject::connect(tabWidget, SIGNAL(tabCloseRequested(int)),
					 this, SLOT(sl_TabWidget_TabCloseRequested(int)));
#endif


	layout = new QBoxLayout(QBoxLayout::LeftToRight);
#if QT_VERSION < 0x040300
	layout->setMargin(0);
#else
	layout->setContentsMargins(0, 0, 0, 0);
#endif

	layout->addWidget(tabWidget);

	setLayout(layout);
}

void NotesTabWidget::OpenNote(Note* note, int position, bool newTab) {
	(void)newTab;

	if (!note) {
		WARNING("Null pointer recieved");
		return;
	}

	if (hash.contains(note)) {
		QWidget* noteWidget = hash.value(note);
		tabWidget->setCurrentWidget(noteWidget);
		return;
	}

	bool showWaitCursor = !note->TextDocumentInitialized();
	if (showWaitCursor) {
		QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
	}
	NoteEditWidget* w = new NoteEditWidget(note);
	if (showWaitCursor) {
		QApplication::restoreOverrideCursor();
	}
	hash.insert(note, w);
	QObject::connect(note, SIGNAL(sg_VisualPropertiesChanged()),
					 this, SLOT(sl_Note_PropertiesChanged()));


	tabWidget->addTab(w, note->GetIcon(), cropStringForTabCaption(note->GetName()));
	tabWidget->setCurrentWidget(w);
	w->ScrollTo(position);
}

void NotesTabWidget::CloseNote(const Note* n) {
	if (!n) {
		WARNING("Null pointer recieved");
		return;
	}

	if (!hash.contains(n)) {
		WARNING("Specified note is not open");
		return;
	}

	QWidget* widget = hash.take(n);
	int tabIndex = tabWidget->indexOf(widget);

	if (tabIndex == -1) {
		WARNING("Could not find associated widget");
		return;
	}

	QObject::disconnect(n, 0, this, 0);
	tabWidget->removeTab(tabIndex);
	delete widget;
}

void NotesTabWidget::ShowFragment(const NoteFragment& fragment) {
	if (!fragment.NotePrt) {
		WARNING("Null pointer recieved");
		return;
	}

	OpenNote(const_cast<Note*>(fragment.NotePrt));

	NoteEditWidget* noteWidget =
			dynamic_cast<NoteEditWidget*>(hash.value(fragment.NotePrt));

	noteWidget->ShowFragment(fragment);
}

void NotesTabWidget::Clear() {
	while (hash.count() > 0) {
		CloseNote(hash.keys().at(0));
	}
}

void NotesTabWidget::sl_Note_PropertiesChanged() {
	const Note* note = qobject_cast<Note*>(QObject::sender());
	if (note == 0) {
		WARNING("Could not find sender note");
		return;
	}

	if (!hash.contains(note)) {
		WARNING("Sender note is not registered");
		QObject::disconnect(note, 0, this, 0);
		return;
	}

	QWidget* widget = hash.value(note);
	int tabIndex = tabWidget->indexOf(widget);
{'fraction_non_alphanumeric': 0.13292975154496153, 'fraction_numerical': 0.007188800605372683, 'mean_word_length': 5.812714776632302, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2094784', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2543, 'n_words': 626}