call_window.cpp 6.52 KB
Newer Older
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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
#include "call_window.hpp"

#include <QMenu>
#include <QStatusBar>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVariant>

#include "../stfl/stringutils.hpp"

namespace cvv
{

namespace controller
{
class ViewController;
}

namespace gui
{

CallWindow::CallWindow(util::Reference<controller::ViewController> controller,
                       size_t id)
    : id{ id }, controller{ controller }
{
	initTabs();
	initFooter();
	setWindowTitle(QString("CVVisual | window no. %1").arg(id));
	setMinimumWidth(600);
	setMinimumHeight(600);
}

void CallWindow::initTabs()
{
	tabWidget = new TabWidget(this);
	tabWidget->setTabsClosable(true);
	tabWidget->setMovable(true);
	setCentralWidget(tabWidget);

	auto *flowButtons = new QHBoxLayout();
	auto *flowButtonsWidget = new QWidget(this);
	tabWidget->setCornerWidget(flowButtonsWidget, Qt::TopLeftCorner);
	flowButtonsWidget->setLayout(flowButtons);
	flowButtons->setAlignment(Qt::AlignLeft | Qt::AlignTop);
	closeButton = new QPushButton("Close", this);
	flowButtons->addWidget(closeButton);
	closeButton->setStyleSheet(
	    "QPushButton {background-color: red; color: white;}");
	closeButton->setToolTip("Close this debugging application.");
	connect(closeButton, SIGNAL(clicked()), this, SLOT(closeApp()));
	fastForwardButton = new QPushButton(">>", this);
	flowButtons->addWidget(fastForwardButton);
	fastForwardButton->setStyleSheet(
	    "QPushButton {background-color: yellow; color: blue;}");
	fastForwardButton->setToolTip(
	    "Fast forward until cvv::finalCall() gets called.");
	connect(fastForwardButton, SIGNAL(clicked()), this,
	        SLOT(fastForward()));
	stepButton = new QPushButton("Step", this);
	flowButtons->addWidget(stepButton);
	stepButton->setStyleSheet(
	    "QPushButton {background-color: green; color: white;}");
	stepButton->setToolTip(
	    "Resume program execution for a next debugging step.");
	connect(stepButton, SIGNAL(clicked()), this, SLOT(step()));
	flowButtons->setContentsMargins(0, 0, 0, 0);
	flowButtons->setSpacing(0);

	auto *tabBar = tabWidget->getTabBar();
	tabBar->setElideMode(Qt::ElideRight);
	tabBar->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(tabBar, SIGNAL(customContextMenuRequested(QPoint)), this,
	        SLOT(contextMenuRequested(QPoint)));
	connect(tabBar, SIGNAL(tabCloseRequested(int)), this,
	        SLOT(tabCloseRequested(int)));
}

void CallWindow::initFooter()
{
	leftFooter = new QLabel();
	rightFooter = new QLabel();
	QStatusBar *bar = statusBar();
	bar->addPermanentWidget(leftFooter, 2);
	bar->addPermanentWidget(rightFooter, 2);
}

void CallWindow::showExitProgramButton()
{
	stepButton->setVisible(false);
	fastForwardButton->setVisible(false);
}

void CallWindow::addTab(CallTab *tab)
{
	tabMap[tab->getId()] = tab;
	QString name = QString("[%1] %2").arg(tab->getId()).arg(tab->getName());
	int index =
	    tabWidget->addTab(tab, stfl::shortenString(name, 20, true, true));
	tabWidget->getTabBar()->setTabData(index, QVariant((int)tab->getId()));
}

size_t CallWindow::getId()
{
	return id;
}

void CallWindow::removeTab(CallTab *tab)
{
	tabMap.erase(tabMap.find(tab->getId()));
	int index = tabWidget->indexOf(tab);
	tabWidget->removeTab(index);
}

void CallWindow::removeTab(size_t tabId)
{
	if (hasTab(tabId))
	{
		removeTab(tabMap[tabId]);
	}
}

void CallWindow::showTab(CallTab *tab)
{
	tabWidget->setCurrentWidget(tab);
}

void CallWindow::showTab(size_t tabId)
{
	if (hasTab(tabId))
	{
		showTab(tabMap[tabId]);
	}
}

void CallWindow::updateLeftFooter(QString newText)
{
	leftFooter->setText(newText);
}

void CallWindow::updateRightFooter(QString newText)
{
	rightFooter->setText(newText);
}

void CallWindow::step()
{
	controller->resumeProgramExecution();
}

void CallWindow::fastForward()
{
	controller->setMode(controller::Mode::FAST_FORWARD);
}

void CallWindow::closeApp()
{
	controller->setMode(controller::Mode::HIDE);
}

bool CallWindow::hasTab(size_t tabId)
{
	return tabMap.count(tabId);
}

void CallWindow::contextMenuRequested(const QPoint &location)
{
	controller->removeEmptyWindows();
	auto tabBar = tabWidget->getTabBar();
	int tabIndex = tabBar->tabAt(location);
	if (tabIndex == tabOffset - 1)
		return;
	QMenu *menu = new QMenu(this);
	connect(menu, SIGNAL(triggered(QAction *)), this,
	        SLOT(contextMenuAction(QAction *)));
	auto windows = controller->getTabWindows();
	menu->addAction(new QAction("Remove call", this));
	menu->addAction(new QAction("Close tab", this));
	menu->addAction(new QAction("Open in new window", this));
	for (auto window : windows)
	{
		if (window->getId() != id)
		{
			menu->addAction(new QAction(
			    QString("Open in '%1'").arg(window->windowTitle()),
			    this));
		}
	}
	currentContextMenuTabId = getCallTabIdByTabIndex(tabIndex);
	menu->popup(tabBar->mapToGlobal(location));
}

void CallWindow::contextMenuAction(QAction *action)
{
	if (currentContextMenuTabId == -1)
	{
		return;
	}
	auto text = action->text();
	if (text == "Open in new window")
	{
		controller->moveCallTabToNewWindow(currentContextMenuTabId);
	}
	else if (text == "Remove call")
	{
		controller->removeCallTab(currentContextMenuTabId, true, true);
	}
	else if (text == "Close tab")
	{
		controller->removeCallTab(currentContextMenuTabId);
	}
	else
	{
		auto windows = controller->getTabWindows();
		for (auto window : windows)
		{
			if (text ==
			    QString("Open in '%1'").arg(window->windowTitle()))
			{
				controller->moveCallTabToWindow(
				    currentContextMenuTabId, window->getId());
				break;
			}
		}
	}
	currentContextMenuTabId = -1;
}

size_t CallWindow::tabCount()
{
	return tabMap.size();
}

std::vector<size_t> CallWindow::getCallTabIds()
{
	std::vector<size_t> ids{};
	for (auto &elem : tabMap)
	{
		ids.push_back(elem.first);
	}
	return ids;
}

void CallWindow::closeEvent(QCloseEvent *event)
{
	controller->removeWindowFromMaps(id);
	// FIXME: tabWidget is already freed sometimes: Use-after-free Bug
	tabWidget->clear();
	for (auto &elem : tabMap)
	{
		controller->removeCallTab(elem.first, true);
	}
	event->accept();
}

void CallWindow::tabCloseRequested(int index)
{
	if (hasTabAtIndex(index))
	{
		controller->removeCallTab(getCallTabIdByTabIndex(index));
	}
	controller->removeEmptyWindows();
}

size_t CallWindow::getCallTabIdByTabIndex(int index)
{
	if (hasTabAtIndex(index))
	{
		auto tabData = tabWidget->getTabBar()->tabData(index);
		bool ok = true;
		size_t callTabId = tabData.toInt(&ok);
		if (ok && tabMap.count(callTabId) > 0)
		{
			return callTabId;
		}
	}
	return 0;
}

bool CallWindow::hasTabAtIndex(int index)
{
	auto tabData = tabWidget->getTabBar()->tabData(index);
	return tabData != 0 && !tabData.isNull() && tabData.isValid();
}
}
}