📄 annotations.cpp.svn-base
字号:
/*************************************************************************** * Copyright (C) 2005 by Enrico Ros <eros.kde@email.it> * * * * This program 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 2 of the License, or * * (at your option) any later version. * ***************************************************************************/// qt/kde includes#include <qdom.h>#include <qcolor.h>#include <qapplication.h>#include <kdebug.h>// local includes#include "annotations.h"//BEGIN AnnotationUtils implementationAnnotation * AnnotationUtils::createAnnotation( const QDomElement & annElement ){ // safety check on annotation element if ( !annElement.hasAttribute( "type" ) ) return 0; // build annotation of given type Annotation * annotation = 0; int typeNumber = annElement.attribute( "type" ).toInt(); switch ( typeNumber ) { case Annotation::AText: annotation = new TextAnnotation( annElement ); break; case Annotation::ALine: annotation = new LineAnnotation( annElement ); break; case Annotation::AGeom: annotation = new GeomAnnotation( annElement ); break; case Annotation::AHighlight: annotation = new HighlightAnnotation( annElement ); break; case Annotation::AStamp: annotation = new StampAnnotation( annElement ); break; case Annotation::AInk: annotation = new InkAnnotation( annElement ); break; } // return created annotation return annotation;}void AnnotationUtils::storeAnnotation( const Annotation * ann, QDomElement & annElement, QDomDocument & document ){ // save annotation's type as element's attribute annElement.setAttribute( "type", (uint)ann->subType() ); // append all annotation data as children of this node ann->store( annElement, document );}QDomElement AnnotationUtils::findChildElement( const QDomNode & parentNode, const QString & name ){ // loop through the whole children and return a 'name' named element QDomNode subNode = parentNode.firstChild(); while( subNode.isElement() ) { QDomElement element = subNode.toElement(); if ( element.tagName() == name ) return element; subNode = subNode.nextSibling(); } // if the name can't be found, return a dummy null element return QDomElement();}//END AnnotationUtils implementation//BEGIN Annotation implementationAnnotation::Style::Style() : opacity( 1.0 ), width( 1.0 ), style( Solid ), xCorners( 0.0 ), yCorners( 0.0 ), marks( 3 ), spaces( 0 ), effect( NoEffect ), effectIntensity( 1.0 ) {}Annotation::Window::Window() : flags( -1 ), width( 0 ), height( 0 ) {}Annotation::Revision::Revision() : annotation( 0 ), scope( Reply ), type( None ) {}Annotation::Annotation() : flags( 0 ) {}Annotation::~Annotation(){ // delete all children revisions if ( revisions.isEmpty() ) return; QLinkedList< Revision >::iterator it = revisions.begin(), end = revisions.end(); for ( ; it != end; ++it ) delete (*it).annotation;}Annotation::Annotation( const QDomNode & annNode ) : flags( 0 ){ // get the [base] element of the annotation node QDomElement e = AnnotationUtils::findChildElement( annNode, "base" ); if ( e.isNull() ) return; // parse -contents- attributes if ( e.hasAttribute( "author" ) ) author = e.attribute( "author" ); if ( e.hasAttribute( "contents" ) ) contents = e.attribute( "contents" ); if ( e.hasAttribute( "uniqueName" ) ) uniqueName = e.attribute( "uniqueName" ); if ( e.hasAttribute( "modifyDate" ) ) modifyDate = QDateTime::fromString( e.attribute("modifyDate"), Qt::ISODate ); if ( e.hasAttribute( "creationDate" ) ) creationDate = QDateTime::fromString( e.attribute("creationDate"), Qt::ISODate ); // parse -other- attributes if ( e.hasAttribute( "flags" ) ) flags = e.attribute( "flags" ).toInt(); if ( e.hasAttribute( "color" ) ) style.color = QColor( e.attribute( "color" ) ); if ( e.hasAttribute( "opacity" ) ) style.opacity = e.attribute( "opacity" ).toDouble(); // parse -the-subnodes- (describing Style, Window, Revision(s) structures) // Note: all subnodes if present must be 'attributes complete' QDomNode eSubNode = e.firstChild(); while ( eSubNode.isElement() ) { QDomElement ee = eSubNode.toElement(); eSubNode = eSubNode.nextSibling(); // parse boundary if ( ee.tagName() == "boundary" ) { boundary=NormalizedRect(ee.attribute( "l" ).toDouble(), ee.attribute( "t" ).toDouble(), ee.attribute( "r" ).toDouble(), ee.attribute( "b" ).toDouble()); } // parse penStyle if not default else if ( ee.tagName() == "penStyle" ) { style.width = ee.attribute( "width" ).toDouble(); style.style = (LineStyle)ee.attribute( "style" ).toInt(); style.xCorners = ee.attribute( "xcr" ).toDouble(); style.yCorners = ee.attribute( "ycr" ).toDouble(); style.marks = ee.attribute( "marks" ).toInt(); style.spaces = ee.attribute( "spaces" ).toInt(); } // parse effectStyle if not default else if ( ee.tagName() == "penEffect" ) { style.effect = (LineEffect)ee.attribute( "effect" ).toInt(); style.effectIntensity = ee.attribute( "intensity" ).toDouble(); } // parse window if present else if ( ee.tagName() == "window" ) { window.flags = ee.attribute( "flags" ).toInt(); window.topLeft.x = ee.attribute( "top" ).toDouble(); window.topLeft.y = ee.attribute( "left" ).toDouble(); window.width = ee.attribute( "width" ).toInt(); window.height = ee.attribute( "height" ).toInt(); window.title = ee.attribute( "title" ); window.summary = ee.attribute( "summary" ); // parse window subnodes QDomNode winNode = ee.firstChild(); for ( ; winNode.isElement(); winNode = winNode.nextSibling() ) { QDomElement winElement = winNode.toElement(); if ( winElement.tagName() == "text" ) window.text = winElement.firstChild().toCDATASection().data(); } } } // get the [revisions] element of the annotation node QDomNode revNode = annNode.firstChild(); for ( ; revNode.isElement(); revNode = revNode.nextSibling() ) { QDomElement revElement = revNode.toElement(); if ( revElement.tagName() != "revision" ) continue; // compile the Revision structure crating annotation Revision rev; rev.scope = (RevScope)revElement.attribute( "revScope" ).toInt(); rev.type = (RevType)revElement.attribute( "revType" ).toInt(); rev.annotation = AnnotationUtils::createAnnotation( revElement ); // if annotation is valid, add revision to internal list if ( rev.annotation ); revisions.append( rev ); }}void Annotation::store( QDomNode & annNode, QDomDocument & document ) const{ // create [base] element of the annotation node QDomElement e = document.createElement( "base" ); annNode.appendChild( e ); // store -contents- attributes if ( !author.isEmpty() ) e.setAttribute( "author", author ); if ( !contents.isEmpty() ) e.setAttribute( "contents", contents ); if ( !uniqueName.isEmpty() ) e.setAttribute( "uniqueName", uniqueName ); if ( modifyDate.isValid() ) e.setAttribute( "modifyDate", modifyDate.toString(Qt::ISODate) ); if ( creationDate.isValid() ) e.setAttribute( "creationDate", creationDate.toString(Qt::ISODate) ); // store -other- attributes if ( flags ) e.setAttribute( "flags", flags ); if ( style.color.isValid() && style.color != Qt::black ) e.setAttribute( "color", style.color.name() ); if ( style.opacity != 1.0 ) e.setAttribute( "opacity", style.opacity ); // Sub-Node-1 - boundary QDomElement bE = document.createElement( "boundary" ); e.appendChild( bE ); bE.setAttribute( "l", (double)boundary.left ); bE.setAttribute( "t", (double)boundary.top ); bE.setAttribute( "r", (double)boundary.right ); bE.setAttribute( "b", (double)boundary.bottom ); // Sub-Node-2 - penStyle if ( style.width != 1 || style.style != Solid || style.xCorners != 0 || style.yCorners != 0.0 || style.marks != 3 || style.spaces != 0 ) { QDomElement psE = document.createElement( "penStyle" ); e.appendChild( psE ); psE.setAttribute( "width", style.width ); psE.setAttribute( "style", (int)style.style ); psE.setAttribute( "xcr", style.xCorners ); psE.setAttribute( "ycr", style.yCorners ); psE.setAttribute( "marks", style.marks ); psE.setAttribute( "spaces", style.spaces ); } // Sub-Node-3 - penEffect if ( style.effect != NoEffect || style.effectIntensity != 1.0 ) { QDomElement peE = document.createElement( "penEffect" ); e.appendChild( peE ); peE.setAttribute( "effect", (int)style.effect ); peE.setAttribute( "intensity", style.effectIntensity ); } // Sub-Node-4 - window if ( window.flags != -1 || !window.title.isEmpty() || !window.summary.isEmpty() || !window.text.isEmpty() ) { QDomElement wE = document.createElement( "window" ); e.appendChild( wE ); wE.setAttribute( "flags", window.flags ); wE.setAttribute( "top", window.topLeft.x ); wE.setAttribute( "left", window.topLeft.y ); wE.setAttribute( "width", window.width ); wE.setAttribute( "height", window.height ); wE.setAttribute( "title", window.title ); wE.setAttribute( "summary", window.summary ); // store window.text as a subnode, because we need escaped data if ( !window.text.isEmpty() ) { QDomElement escapedText = document.createElement( "text" ); wE.appendChild( escapedText ); QDomCDATASection textCData = document.createCDATASection( window.text ); escapedText.appendChild( textCData ); } } // create [revision] element of the annotation node (if any) if ( revisions.isEmpty() ) return; // add all revisions as children of revisions element QLinkedList< Revision >::const_iterator it = revisions.begin(), end = revisions.end(); for ( ; it != end; ++it ) { // create revision element const Revision & revision = *it; QDomElement r = document.createElement( "revision" ); annNode.appendChild( r ); // set element attributes r.setAttribute( "revScope", (int)revision.scope ); r.setAttribute( "revType", (int)revision.type ); // use revision as the annotation element, so fill it up AnnotationUtils::storeAnnotation( revision.annotation, r, document ); }}//END AnnotationUtils implementation/** TextAnnotation [Annotation] */TextAnnotation::TextAnnotation() : Annotation(), textType( Linked ), textIcon( "Comment" ), inplaceAlign( 0 ), inplaceIntent( Unknown ){}TextAnnotation::TextAnnotation( const QDomNode & node ) : Annotation( node ), textType( Linked ), textIcon( "Comment" ), inplaceAlign( 0 ), inplaceIntent( Unknown ){ // loop through the whole children looking for a 'text' element QDomNode subNode = node.firstChild(); while( subNode.isElement() ) { QDomElement e = subNode.toElement(); subNode = subNode.nextSibling(); if ( e.tagName() != "text" ) continue; // parse the attributes if ( e.hasAttribute( "type" ) ) textType = (TextAnnotation::TextType)e.attribute( "type" ).toInt(); if ( e.hasAttribute( "icon" ) ) textIcon = e.attribute( "icon" ); if ( e.hasAttribute( "font" ) ) textFont.fromString( e.attribute( "font" ) ); if ( e.hasAttribute( "align" ) ) inplaceAlign = e.attribute( "align" ).toInt(); if ( e.hasAttribute( "intent" ) ) inplaceIntent = (TextAnnotation::InplaceIntent)e.attribute( "intent" ).toInt(); // parse the subnodes QDomNode eSubNode = e.firstChild(); while ( eSubNode.isElement() ) { QDomElement ee = eSubNode.toElement(); eSubNode = eSubNode.nextSibling(); if ( ee.tagName() == "escapedText" ) { inplaceText = ee.firstChild().toCDATASection().data(); } else if ( ee.tagName() == "callout" ) { inplaceCallout[0].x = ee.attribute( "ax" ).toDouble(); inplaceCallout[0].y = ee.attribute( "ay" ).toDouble(); inplaceCallout[1].x = ee.attribute( "bx" ).toDouble(); inplaceCallout[1].y = ee.attribute( "by" ).toDouble(); inplaceCallout[2].x = ee.attribute( "cx" ).toDouble(); inplaceCallout[2].y = ee.attribute( "cy" ).toDouble(); } } // loading complete break; }}void TextAnnotation::store( QDomNode & node, QDomDocument & document ) const{ // recurse to parent objects storing properties Annotation::store( node, document ); // create [text] element QDomElement textElement = document.createElement( "text" ); node.appendChild( textElement ); // store the optional attributes if ( textType != Linked ) textElement.setAttribute( "type", (int)textType ); if ( textIcon != "Comment" ) textElement.setAttribute( "icon", textIcon ); if ( textFont != QApplication::font() ) textElement.setAttribute( "font", textFont.toString() ); if ( inplaceAlign ) textElement.setAttribute( "align", inplaceAlign ); if ( inplaceIntent != Unknown ) textElement.setAttribute( "intent", (int)inplaceIntent ); // Sub-Node-1 - escapedText if ( !inplaceText.isEmpty() )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -