Esempio n. 1
0
void TrackView::keyPressEvent(QKeyEvent *event)
{
	SyncDocument *doc = getDocument();

	if (!readOnly && lineEdit->isVisible()) {
		switch (event->key()) {
		case Qt::Key_Up:
		case Qt::Key_Down:
		case Qt::Key_Left:
		case Qt::Key_Right:
		case Qt::Key_PageUp:
		case Qt::Key_PageDown:
		case Qt::Key_Home:
		case Qt::Key_End:
		case Qt::Key_Space:
			editEnterValue();
			return;
		}
	}

	bool shiftDown = (event->modifiers() & Qt::ShiftModifier) != 0;
	bool ctrlDown = (event->modifiers() & Qt::ControlModifier) != 0;
	bool selecting = shiftDown;

	if (lineEdit->isHidden()) {
		switch (event->key()) {
		case Qt::Key_Backtab:
			if (ctrlDown) {
				// the main window will handle this
				event->ignore();
				return;
			}
			selecting = false;
			// FALLTHROUGH
		case Qt::Key_Left:
			if (ctrlDown) {
				if (0 < editTrack)
					page->swapTrackOrder(editTrack, editTrack - 1);
				else
					QApplication::beep();
			}
			if (0 != getTrackCount())
				setEditTrack(editTrack - 1, selecting);
			else
				QApplication::beep();
			return;

		case Qt::Key_Tab:
			if (ctrlDown) {
				// the main window will handle this
				event->ignore();
				return;
			}
			selecting = false;
			// FALLTHROUGH
		case Qt::Key_Right:
			if (ctrlDown) {
				if (getTrackCount() > editTrack + 1)
					page->swapTrackOrder(editTrack, editTrack + 1);
				else
					QApplication::beep();
			}
			if (0 != getTrackCount())
				setEditTrack(editTrack + 1, selecting);
			else
				QApplication::beep();
			return;
		}
	}

	if (lineEdit->isHidden()) {
		if (!readOnly && ctrlDown) {
			switch (event->key()) {
			case Qt::Key_Up:
				if (getTrackCount() > editTrack)
					editBiasValue(shiftDown ? 0.1f : 1.0f);
				else
					QApplication::beep();
				return;

			case Qt::Key_Down:
				if (getTrackCount() > editTrack)
					editBiasValue(shiftDown ? -0.1f : -1.0f);
				else
					QApplication::beep();
				return;

			case Qt::Key_PageUp:
				editBiasValue(shiftDown ? 100.0f : 10.0f);
				return;

			case Qt::Key_PageDown:
				editBiasValue(shiftDown ? -100.0f : -10.0f);
				return;
			}
		}

		switch (event->key()) {
		case Qt::Key_Up:
			setEditRow(editRow - 1, selecting);
			return;

		case Qt::Key_Down:
			setEditRow(editRow + 1, selecting);
			return;

		case Qt::Key_PageUp:
			setEditRow(editRow - 0x10, selecting);
			return;

		case Qt::Key_PageDown:
			setEditRow(editRow + 0x10, selecting);
			return;

		case Qt::Key_Home:
			if (ctrlDown)
				setEditTrack(0, false);
			else
				setEditRow(0, selecting);
			return;

		case Qt::Key_End:
			if (ctrlDown)
				setEditTrack(getTrackCount() - 1, false);
			else
				setEditRow(getRows() - 1, selecting);
			return;
		}
	}

	switch (event->key()) {
	case Qt::Key_Delete:
		editClear();
		return;

	case Qt::Key_Cancel:
	case Qt::Key_Escape:
		if (!readOnly && lineEdit->isVisible()) {
			// return to old value (i.e don't clear)
			lineEdit->hide();
			setFocus();
			QApplication::beep();
		}
		return;

	case Qt::Key_I:
		editToggleInterpolationType();
		return;

	case Qt::Key_K:
		doc->toggleRowBookmark(getEditRow());
		return;
	}

	if (!readOnly && !ctrlDown && lineEdit->isHidden() && event->text().length() && doc->getTrackCount()) {
		// no line-edit, check if input matches a double
		QString str = event->text();
		int pos = 0;
		if (lineEdit->validator()->validate(str, pos) != QValidator::Invalid) {
			lineEdit->move(getPhysicalX(getEditTrack()), getPhysicalY(getEditRow()));
			lineEdit->resize(trackWidth, rowHeight);
			lineEdit->setText("");
			lineEdit->show();
			lineEdit->event(event);
			lineEdit->setFocus();
			return;
		}
	}

	event->ignore();
}
Esempio n. 2
0
SyncDocument *SyncDocument::load(const QString &fileName)
{
	SyncDocument *ret = new SyncDocument;
	ret->fileName = fileName;

	QFile file(fileName);
	if (!file.open(QIODevice::ReadOnly)) {
		QMessageBox::critical(NULL, "Error", file.errorString());
		return NULL;
	}

	QDomDocument doc;
	QString err;
	if (!doc.setContent(&file, &err)) {
		file.close();
		QMessageBox::critical(NULL, "Error", err);
		return NULL;
	}
	file.close();

	QDomNamedNodeMap attribs = doc.documentElement().attributes();
	QDomNode rowsParam = attribs.namedItem("rows");
	if (!rowsParam.isNull()) {
		QString rowsString = rowsParam.nodeValue();
		ret->setRows(rowsString.toInt());
	}

	QDomNodeList trackNodes =
	    doc.documentElement().elementsByTagName("track");
	for (int i = 0; i < int(trackNodes.length()); ++i) {
		QDomNode trackNode = trackNodes.item(i);
		QDomNamedNodeMap attribs = trackNode.attributes();

		QString name = attribs.namedItem("name").nodeValue();

		// look up track-name, create it if it doesn't exist
		SyncTrack *t = ret->findTrack(name.toUtf8());
		if (!t)
			t = ret->createTrack(name.toUtf8().constData());

		QDomNodeList rowNodes = trackNode.childNodes();
		for (int i = 0; i < int(rowNodes.length()); ++i) {
			QDomNode keyNode = rowNodes.item(i);
			QString baseName = keyNode.nodeName();
			if (baseName == "key") {
				QDomNamedNodeMap rowAttribs = keyNode.attributes();
				QString rowString = rowAttribs.namedItem("row").nodeValue();
				QString valueString = rowAttribs.namedItem("value").nodeValue();
				QString interpolationString = rowAttribs.namedItem("interpolation").nodeValue();

				SyncTrack::TrackKey k;
				k.row = rowString.toInt();
				k.value = valueString.toFloat();
				k.type = SyncTrack::TrackKey::KeyType(interpolationString.toInt());

				Q_ASSERT(!t->isKeyFrame(k.row));
				t->setKey(k);
			}
		}
	}

	// YUCK: gathers from entire document
	QDomNodeList bookmarkNodes =
	    doc.documentElement().elementsByTagName("bookmark");
	for (int i = 0; i < int(bookmarkNodes.length()); ++i) {
		QDomNode bookmarkNode =
		    bookmarkNodes.item(i);
		QDomNamedNodeMap bookmarkAttribs =
		    bookmarkNode.attributes();
		QString str =
		    bookmarkAttribs.namedItem("row").nodeValue();
		int row = str.toInt();
		ret->toggleRowBookmark(row);
	}

	return ret;
}