taskbusplatformfrm.cpp 10.5 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
1 2 3 4 5 6 7 8 9 10 11 12
#include "taskbusplatformfrm.h"
#include "ui_taskbusplatformfrm.h"
#include <QCloseEvent>
#include <QDir>
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QMdiSubWindow>
#include <QSettings>
#include <QDateTime>
#include "pdesignerview.h"
13
#include "handbookview.h"
丁劲犇's avatar
丁劲犇 已提交
14 15 16 17 18 19 20 21 22 23
#include "watchdog/tbwatchdog.h"
#include "dlgabout.h"

int taskBusPlatformFrm::m_doc_ins = 0;

taskBusPlatformFrm::taskBusPlatformFrm(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::taskBus),
	m_pTrayIcon(new QSystemTrayIcon(this)),
	m_pMsgModel(new QStandardItemModel(this)),
D
dev 已提交
24
	m_pCmdViewModel(new QStandardItemModel(this)),
丁劲犇's avatar
丁劲犇 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
	m_pClassModel(new QStandardItemModel(this))
{
	ui->setupUi(this);
	setCentralWidget(ui->mdiArea);
	//全部模块 Create Module for All taskModules
	m_pRefModule = m_toolModules[tr("All")] = new taskModule(true,this);
	ui->listView_modules->setModel(m_toolModules[tr("All")]);

	//类别 ComboBox for classes
	m_pClassModel->appendRow(new QStandardItem(tr("All")));
	ui->comboBox_class->setModel(m_pClassModel);
	//消息 message module
	ui->listView_messages->setModel(m_pMsgModel);
D
dev 已提交
38
	ui->listView_commands->setModel(m_pCmdViewModel);
丁劲犇's avatar
丁劲犇 已提交
39 40 41 42 43
	//状态栏 status bar
	m_pStatus = new QLabel(this);
	statusBar()->addWidget(m_pStatus);

	m_nTmid = startTimer(1000);
D
dev 已提交
44 45 46
	tabifyDockWidget(ui->dockWidget_props,ui->dockWidget_cmd);
	tabifyDockWidget(ui->dockWidget_props,ui->dockWidget_watch);
	tabifyDockWidget(ui->dockWidget_props,ui->dockWidget_message);
丁劲犇's avatar
丁劲犇 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	m_iconTray[0].addFile(":/taskBus/images/ticon1.png");
	m_iconTray[1].addFile(":/taskBus/images/ticon2.png");
	m_pTrayIcon->setIcon(m_iconTray[0]);
	m_pTrayIcon->show();

	QMenu * me = new QMenu(this);
	me->addAction(ui->actionhideWindow);
	me->addAction(ui->action_About);
	m_pTrayIcon->setContextMenu(me);
	connect(m_pTrayIcon,&QSystemTrayIcon::activated,this,&taskBusPlatformFrm::slot_traymessage);
	ui->menu_View->insertAction(ui->actionhideWindow,ui->dockWidget_message->toggleViewAction());
	ui->menu_View->insertAction(ui->actionhideWindow,ui->dockWidget_modules->toggleViewAction());
	ui->menu_View->insertAction(ui->actionhideWindow,ui->dockWidget_props->toggleViewAction());
	ui->menu_View->insertAction(ui->actionhideWindow,ui->dockWidget_watch->toggleViewAction());

D
dev 已提交
62
	m_bShowCmds = false;
丁劲犇's avatar
丁劲犇 已提交
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
}

taskBusPlatformFrm::~taskBusPlatformFrm()
{
	QList<QMdiSubWindow *> lst = ui->mdiArea->subWindowList();
	foreach (QMdiSubWindow * sub, lst)
	{
		PDesignerView * dv = qobject_cast<PDesignerView *>(sub->widget());
		if (dv)
			dv->stop();
	}
	delete ui;
}

void taskBusPlatformFrm::slot_traymessage(QSystemTrayIcon::ActivationReason r)
{
	switch (r)
	{
	case QSystemTrayIcon::DoubleClick:
		ui->actionhideWindow->setChecked(false);
		break;
	default:
		break;
	}
}

void taskBusPlatformFrm::timerEvent(QTimerEvent *event)
{
	static int pp = 0;
	if (m_nTmid==event->timerId())
	{
		extern QAtomicInt  g_totalrev, g_totalsent;
丁劲犇's avatar
丁劲犇 已提交
95
		QString s = QString().asprintf("down %.2lf Mbps, up %.2lf Mbps",
丁劲犇's avatar
丁劲犇 已提交
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
									  g_totalrev * 8.0 / 1024 / 1024,
									  g_totalsent * 8.0 / 1024 / 1024
									  );
		m_pStatus->setText(s);
		if (g_totalrev>0 || g_totalrev>0)
		{
			++pp;
			m_pTrayIcon->setIcon(m_iconTray[pp%2]);
		}
		else if (pp)
		{
			pp = 0;
			m_pTrayIcon->setIcon(m_iconTray[0]);
		}
		g_totalsent = 0;
		g_totalrev = 0;
	}
}

QString taskBusPlatformFrm::inifile()
{
	return QCoreApplication::applicationFilePath()+".ini";
}

void taskBusPlatformFrm::slot_showPropModel(QObject * objModel)
{
	taskModule * pm = qobject_cast<taskModule *>(objModel);
	if (pm)
	{
		ui->treeView_props->setModel(pm);
		ui->treeView_props->collapseAll();
		ui->treeView_props->expand(pm->index(0,0));
		ui->treeView_props->expand(pm->paraIndex());
		ui->treeView_props->scrollToBottom();
		//ui->treeView_props->expandAll();
	}
	else
		ui->treeView_props->setModel(0);
}

void taskBusPlatformFrm::on_action_About_triggered()
{
	show();
	DlgAbout about(this);
	if (about.exec()==QDialog::Accepted)
		QApplication::aboutQt();
}

void taskBusPlatformFrm::on_action_Start_project_triggered()
{
	QMdiSubWindow * sub = ui->mdiArea->activeSubWindow();
	if (sub)
	{
		PDesignerView * dv = qobject_cast<PDesignerView *>(sub->widget());
		if (dv)
		{
			dv->run();
			on_mdiArea_subWindowActivated(sub);
		}
	}
}


void taskBusPlatformFrm::on_action_stop_project_triggered()
{
	QMdiSubWindow * sub = ui->mdiArea->activeSubWindow();
	if (sub)
	{
		PDesignerView * dv = qobject_cast<PDesignerView *>(sub->widget());
		if (dv)
		{
			dv->stop();
			on_mdiArea_subWindowActivated(sub);
		}
	}

}

void taskBusPlatformFrm::load_default_modules()
{
	QString DefaultFile = QCoreApplication::applicationDirPath() + "/default_mods.text";
	QFile fin(DefaultFile);
	if (fin.open(QIODevice::ReadOnly)==false)
179 180 181
	{
		QThread::msleep(2000);
		emit hideSplash();
丁劲犇's avatar
丁劲犇 已提交
182
		return;
183
	}
丁劲犇's avatar
丁劲犇 已提交
184 185 186 187 188 189 190 191 192 193
	QTextStream st(&fin);
	QStringList lstNames;
	while (st.atEnd()==false)
	{
		lstNames<< st.readLine();
	}
	fin.close();
	m_pTrayIcon->showMessage(tr("Init Modules..."),tr("Init modules from default_mods.text"),QSystemTrayIcon::Information, 1000);
	load_modules(lstNames);
	m_pTrayIcon->showMessage(tr("Succeed."),tr("Init modules from default_mods.text succeed!"),QSystemTrayIcon::Information, 2000);
194
	QThread::msleep(1000);
丁劲犇's avatar
丁劲犇 已提交
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	emit hideSplash();
}
void taskBusPlatformFrm::save_default_modules()
{
	const QSet<QString> & s = refModule()->full_paths();

	QString DefaultFile = QCoreApplication::applicationDirPath() + "/default_mods.text";
	QFile fin(DefaultFile);
	if (fin.open(QIODevice::WriteOnly)==false)
		return;
	QTextStream st(&fin);
	QDir dir(QCoreApplication::applicationDirPath());
	foreach (QString sf, s) {
		QString strRelPath;
		if (QDir::isAbsolutePath(sf)==true)
			strRelPath  =  dir.relativeFilePath(sf);
		else
			strRelPath = sf;
		if (strRelPath.length()>0 && strRelPath.length()<sf.length())
			st<<strRelPath<<"\n";
		else
			st<<sf<<"\n";
	}

	fin.close();
}


void taskBusPlatformFrm::on_mdiArea_subWindowActivated(QMdiSubWindow *arg1)
{
	if (!arg1)
	{
		ui->action_Start_project->setEnabled(false);
		ui->action_stop_project->setEnabled(false);
		ui->action_Save_Project->setEnabled(false);
		ui->dockWidget_props->setEnabled(false);
		ui->treeView_props->setModel(0);
		return;
	}
	else
	{
		ui->action_Save_Project->setEnabled(true);
	}
	PDesignerView * dv = qobject_cast<PDesignerView *>(arg1->widget());
	if (dv)
	{
		int noden = dv->selectedNode();
		if (noden<0||noden>=dv->project()->vec_cells().size())
			ui->treeView_props->setModel(0);
		else
		{
			taskCell * c = dv->project()->vec_cells()[noden];
			taskModule * m = dynamic_cast<taskModule *>(c);
			if (m!=ui->treeView_props->model() && m)
			{
				ui->treeView_props->setModel(m);
				ui->treeView_props->collapseAll();
				ui->treeView_props->expand(m->index(0,0));
				ui->treeView_props->expand(m->paraIndex());
				ui->treeView_props->scrollToBottom();
			}
			else if (!m)
				ui->treeView_props->setModel(0);
		}

		ui->action_Start_project->setEnabled(!dv->is_running());
		ui->action_stop_project->setEnabled(dv->is_running());
		ui->dockWidget_props->setEnabled(!dv->is_running());
		dv->setEnabled(!dv->is_running());
	}
}

void taskBusPlatformFrm::on_comboBox_class_currentIndexChanged(int index)
{
	if (index>=0 && index<m_pClassModel->rowCount())
	{
		QString name = m_pClassModel->data(m_pClassModel->index(index,0)).toString();
		if (m_toolModules.contains(name))
			ui->listView_modules->setModel(m_toolModules[name]);
	}
}

void taskBusPlatformFrm::closeEvent(QCloseEvent * event)
{
	bool bModified = false;
	bool bRunning = false;
	QStringList lsN = m_activePagesFileName.keys();
	foreach (QString k, lsN)
	{
		QWidget * w = m_activePagesFileName[k]->widget();
		if (w)
		{
			PDesignerView * v = qobject_cast<PDesignerView*>(w);
			if(v)
			{
				bModified = bModified || v->modified();
				bRunning = bRunning || v->project()->isRunning();
			}
		}
	}
	if (bRunning)
	{
		QMessageBox::information(this,tr("Still running"),tr("Project is still running, please stop all projects first."),QMessageBox::Ok);
		event->ignore();
		return;
	}
	if (bModified)
	{
		if (QMessageBox::information(this,tr("Close without saving?"),tr("Project has been modified, Close it anyway?"),
									 QMessageBox::Ok,
									 QMessageBox::Cancel)!=QMessageBox::Ok)
		{
			event->ignore();
			return;
		}
	}
	event->accept();
312
	this->m_pTrayIcon->hide();
丁劲犇's avatar
丁劲犇 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326
}

void taskBusPlatformFrm::on_actionhideWindow_toggled(bool arg1)
{
	if (arg1)
		hide();
	else
		show();
}



void taskBusPlatformFrm::on_listView_modules_doubleClicked(const QModelIndex &index)
{
327
	if (ui->listView_modules->model())
丁劲犇's avatar
丁劲犇 已提交
328
	{
329 330 331 332 333 334 335 336 337 338
		taskModule * mod = qobject_cast<taskModule *>(ui->listView_modules->model());
		if (!mod)
			return;
		int r = index.row();
		QStringList functions = mod->function_names();
		if (r<0 || r>=functions.size())
			return;
		QString func = functions.at(r);
		QString exe = mod->function_exec(func);
		load_doucment(func,exe);
丁劲犇's avatar
丁劲犇 已提交
339 340

	}
341

丁劲犇's avatar
丁劲犇 已提交
342
}
D
dev 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

void taskBusPlatformFrm::on_checkBox_showCmd_stateChanged(int /*arg1*/)
{
	m_bShowCmds = ui->checkBox_showCmd->isChecked();
}
void taskBusPlatformFrm::slot_cmd_show(QMap<QString,QVariant> cmd)
{
	if (m_bShowCmds==false)
		return;
	const int keepRows = 128;
	QString prefix = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
	QString str = taskCell::map_to_string(cmd);
	m_pCmdViewModel->appendRow(new QStandardItem(prefix+">"+str));
	if (m_pCmdViewModel->rowCount()>keepRows)
		m_pCmdViewModel->removeRows(0,m_pCmdViewModel->rowCount()-keepRows);
	ui->listView_commands->scrollToBottom();
}
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

void  taskBusPlatformFrm::load_doucment(QString func,QString exe)
{
	//OpenDocuments
	QFileInfo info(exe);
	QString baseName = info.path()+"/"+
			info.completeBaseName();
	QString urlstr;
	QString FailedString;
	FailedString += exe+".md" + "\n\n";
	FailedString += baseName+".md" + "\n\n";
	FailedString += baseName+".documents/index.md" + "\n\n";
	if (QFileInfo::exists(exe+".md"))
		urlstr = exe+".md";
	else if (QFileInfo::exists(baseName+".md"))
		urlstr = baseName+".md";
	else if (QFileInfo::exists(baseName+".documents/index.md"))
		urlstr = baseName+".documents/index.md";

	QList<QMdiSubWindow *> AllSubWnds = ui->mdiArea->subWindowList();
	QString title = tr("DOC:") + func;
	//Find existing window
	QMdiSubWindow * wnd = nullptr;
	foreach (QMdiSubWindow * w, AllSubWnds)
	{
		if (w->windowTitle()==title)
		{
			wnd = w;
			ui->mdiArea->setActiveSubWindow(w);
			break;
		}
	}
	if (!wnd )
	{
		HandbookView * view  = new HandbookView(this);
		if (view)
		{
			view->setWindowTitle(title);
			wnd = ui->mdiArea->addSubWindow(view);
			view->show();
			view->setUrl(urlstr,FailedString);
			wnd->setWindowTitle(title);
		}
	}

}

void taskBusPlatformFrm::on_action_Help_triggered()
{
	load_doucment("taskBus",QCoreApplication::applicationFilePath());
}