void findA(QDomElement element, QList<QDomElement> & aElements) { if (element.tagName().compare("a", Qt::CaseInsensitive) == 0) { aElements.append(element); return; } QDomElement c = element.firstChildElement(); while (!c.isNull()) { findA(c, aElements); c = c.nextSiblingElement(); } }
void Note::linkDialog() { QTextCursor textCursor = m_graphicsTextItem->textCursor(); bool gotUrl = false; if (textCursor.anchor() == textCursor.selectionStart()) { // the selection returns empty since we're between characters // so select one character forward or one character backward // to see whether we're in a url int wasAnchor = textCursor.anchor(); bool atEnd = textCursor.atEnd(); bool atStart = textCursor.atStart(); if (!atStart) { textCursor.setPosition(wasAnchor - 1, QTextCursor::KeepAnchor); QString html = textCursor.selection().toHtml(); if (UrlTag.indexIn(html) >= 0) { gotUrl = true; } } if (!gotUrl && !atEnd) { textCursor.setPosition(wasAnchor + 1, QTextCursor::KeepAnchor); QString html = textCursor.selection().toHtml(); if (UrlTag.indexIn(html) >= 0) { gotUrl = true; } } textCursor.setPosition(wasAnchor, QTextCursor::MoveAnchor); } else { QString html = textCursor.selection().toHtml(); DebugDialog::debug(html); if (UrlTag.indexIn(html) >= 0) { gotUrl = true; } } LinkDialog ld; QString originalText; QString originalUrl; if (gotUrl) { originalUrl = UrlTag.cap(1); ld.setUrl(originalUrl); QString html = m_graphicsTextItem->toHtml(); // assumes html is in xml form QString errorStr; int errorLine; int errorColumn; QDomDocument domDocument; if (!domDocument.setContent(html, &errorStr, &errorLine, &errorColumn)) { return; } QDomElement root = domDocument.documentElement(); if (root.isNull()) { return; } if (root.tagName() != "html") { return; } DebugDialog::debug(html); QList<QDomElement> aElements; findA(root, aElements); foreach (QDomElement a, aElements) { // TODO: if multiple hrefs point to the same url this will only find the first one QString href = a.attribute("href"); if (href.isEmpty()) { href = a.attribute("HREF"); } if (href.compare(originalUrl) == 0) { QString text; if (TextUtils::findText(a, text)) { ld.setText(text); break; } else { return; } } } }
int main(int argc, char *argv[]) { if(argc != 5 || argc == 1) // User didn't use correct number of args { printUsage(); exit(EXIT_FAILURE); } int flags; bool aFlag = false, bFlag = false, cFlag = false; double a = 0.0, b = 0.0, c = 0.0; while ((flags = getopt(argc, argv, "a:b:c:h")) != -1) // Go through arguments used { switch(flags) { case 'h': // Help, print and exit printUsage(); exit(EXIT_SUCCESS); case 'a': // Store the 'a' side and set aFlag if(aFlag) { moreThanOneSide(a); } else { aFlag = true; a = atof(optarg); } break; case 'b': // Store the 'b' side and set bFlag if(bFlag) { moreThanOneSide(b); } else { bFlag = true; b = atof(optarg); } break; case 'c': // Store the 'c' side and set cFlag if(cFlag) { moreThanOneSide(c); } else { cFlag = true; c = atof(optarg); } break; } } if(aFlag && bFlag) // Calculate and print result for side C { c = findC(a, b); printf("The length of side 'c' is %G\n", c); } else if(aFlag && cFlag) { // Calculate and print result for side B b = findB(a, c); printf("The length of side 'b' is %G\n", b); } else if (bFlag && cFlag) { // Calculate and print result for side A a = findA(b, c); printf("The length of side 'a' is %G\n", a); } exit(EXIT_SUCCESS); }