001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.web.layout;
031
032import org.openimaj.math.geometry.shape.Rectangle;
033
034import com.trolltech.qt.webkit.QWebElement;
035
036/**
037 * Information about a DOM element
038 * 
039 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
040 *
041 */
042public class ElementInfo {
043        QWebElement element;
044        Rectangle bounds;
045        boolean isContent = false;
046        boolean isInsideContent = false;
047        
048        /**
049         * @return the element
050         */
051        public QWebElement getElement() {
052                return element;
053        }
054        
055        /**
056         * @param element the element to set
057         */
058        public void setElement(QWebElement element) {
059                this.element = element;
060        }
061        
062        /**
063         * @return the bounds
064         */
065        public Rectangle getBounds() {
066                return bounds;
067        }
068        
069        /**
070         * @param bounds the bounds to set
071         */
072        public void setBounds(Rectangle bounds) {
073                this.bounds = bounds;
074        }
075        
076        /**
077         * @return the isContent
078         */
079        public boolean isContent() {
080                return isContent;
081        }
082        
083        /**
084         * @param isContent the isContent to set
085         */
086        public void setContent(boolean isContent) {
087                this.isContent = isContent;
088        }
089        
090        /**
091         * @return the isInsideContent
092         */
093        public boolean isInsideContent() {
094                return isInsideContent;
095        }
096        
097        /**
098         * @param isInsideContent the isInsideContent to set
099         */
100        public void setInsideContent(boolean isInsideContent) {
101                this.isInsideContent = isInsideContent;
102        }
103        
104        /**
105         * @return The ID of the element. Auto-generated if if doesn't hove one.
106         */
107        public String getElementId() {
108                String id = element.attribute("id");
109                
110                if (id == null || id.contains("__gen_id_"))
111                        return "";
112                
113                return id;
114        }
115        
116        @Override
117        public String toString() {
118                return String.format("ElementInfo[%s](%d,%d,%d,%d)", element.tagName(), (int)bounds.x, (int)bounds.y, (int)bounds.width, (int)bounds.height);
119        }
120        
121        /**
122         * @return Get the column headings for CSV output
123         */
124        public static String getCSVHeader() {
125                return String.format("%s,%s,%s,%s,%s,%s,%s,%s", "tagName", "id", "x", "y", "width", "height","isContent","isInsideContent");
126        }
127        
128        /**
129         * @return Write element to CSV data
130         */
131        public String toCSVString() {
132                return String.format("%s,%s,%d,%d,%d,%d,%s,%s", element.tagName(), 
133                                getElementId(), 
134                                (int)bounds.x, (int)bounds.y, (int)bounds.width, (int)bounds.height,isContent,isInsideContent);
135        }
136}