MainPage.qml 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* This file is part of the KDE project
 * Copyright (C) 2012 Arjen Hiemstra <ahiemstra@heimr.nl>
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

import QtQuick 1.1
import org.krita.sketch 1.0
21
import org.krita.sketch.components 1.0
22 23 24 25 26 27 28 29 30
import "panels"

Page {
    property string pageName: "MainPage";
    SketchView {
        id: sketchView;
        width: parent.width;
        height: parent.height;

31 32
        file: parent.initialFile;

33 34 35 36 37 38 39 40
        onInteractionStarted: { panelBar.collapse(); Krita.VirtualKeyboardController.requestHideKeyboard(); }
        onLoadingFinished: {
            loadingDialog.hide("Done!");
            savingDialog.hide("Done!");
        }
        onSavingFinished: {
            loadingDialog.hide("Done!");
            savingDialog.hide("Done!");
41 42 43 44 45 46 47 48

            if (d.saveRequested) {
                d.loadNewFile();
            }

            if (d.closeRequested) {
                d.closeWindow();
            }
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        }
        onProgress: {
            if (value === -1 || value === 100) {
                loadingDialog.hide("Done!");
                savingDialog.hide("Done!");
            }
            loadingDialog.progress = value;
            savingDialog.progress = value;
        }
        onViewChanged: if (window.sketchKisView !== undefined) { window.sketchKisView = view; }
    }
    Connections {
        target: window;
        onSwitchedToSketch: sketchView.activate();
    }


    ToolManager {
        id: toolManager;
        view: sketchView.view;
    }

    LayerModel {
        id: layerModel;
        // Notice - the model needs to know about the engine before the view, hence it is set first
        // This could be rectified, but for now know that the order here is important.
        engine: QMLEngine;
        view: sketchView.view;
    }

    PanelBar {
        id: panelBar;
81
        panelHeight: parent.height;
82 83 84 85 86 87 88 89
        width: parent.width;
    }

    NewImagePanel {
        id: newPanel;
        anchors.left: parent.left;
        width: Constants.GridWidth * 4;
        height: parent.height;
90

91
        onClicked: d.beginCreateNewFile(options);
92 93 94 95 96 97 98 99
    }

    OpenImagePanel {
        id: openPanel;
        anchors.left: parent.left;
        width: Constants.GridWidth * 4;
        height: parent.height;

100
        onClicked: d.beginOpenFile(file);
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
    }

    MenuPanel {
        id: menuPanel;

        anchors.bottom: parent.bottom;

        width: parent.width;
        z: 10;

        newButtonChecked: !newPanel.collapsed;
        openButtonChecked: !openPanel.collapsed;

        onCollapsedChanged: if ( collapsed ) {
            newPanel.collapsed = true;
            openPanel.collapsed = true;
        }

        onButtonClicked: {
            switch( button ) {
                case "new": {
                    d.previousFile = Settings.currentFile;
                    newPanel.collapsed = !newPanel.collapsed;
                    openPanel.collapsed = true;
                }
                case "open": {
                    d.previousFile = Settings.currentFile;
                    openPanel.collapsed = !openPanel.collapsed;
                    newPanel.collapsed = true;
                }
                case "save":
                    if (!Settings.temporaryFile) {
                        savingDialog.show("Saving file...");
                        sketchView.save();
                    } else {
                        pageStack.push( saveAsPage, { view: sketchView } );
                    }
                case "saveAs":
                    pageStack.push( saveAsPage, { view: sketchView } );
                case "settings":
                    pageStack.push( settingsPage );
                case "help":
                    pageStack.push( helpPage );
                case "undo":
                    sketchView.undo();
                case "redo":
                    sketchView.redo();
                case "minimize":
                    Krita.Window.minimize();
                case "close":
                    Krita.Window.close();
                case "zoomIn":
                    sketchView.zoomIn();
                case "zoomOut":
                    sketchView.zoomOut();
                case "switchToDesktop":
                    switchToDesktopAction.trigger();
            }
        }
    }

    MessageStack {
        id: messageStack;
        anchors {
            horizontalCenter: parent.horizontalCenter;
            bottom: menuPanel.top;
            bottomMargin: Constants.GridHeight;
        }
        Connections {
            target: sketchView;
171
            onFloatingMessageRequested: {
172
                if(message == undefined || message.startsWith == undefined)
173 174
                    return;

175 176 177 178 179
                if(message.startsWith("Zoom") || message.startsWith("Rotation"))
                    return;
                
                messageStack.showMessage(message, iconName);
            }
180 181 182
        }
    }

183 184 185 186 187 188 189 190 191 192 193 194
    ToolOverlayPanel {
        id: toolOverlay;
        anchors {
            left: menuPanel.left;
            leftMargin: (Constants.IsLandscape ? Constants.GridWidth * 4: Constants.GridWidth * 2) + Constants.DefaultMargin;
            right: menuPanel.right;
            rightMargin: (Constants.IsLandscape ? 0 : Constants.GridWidth * 2) + Constants.DefaultMargin;
            bottom: menuPanel.top;
            bottomMargin: Constants.DefaultMargin;
        }
    }

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
    Dialog {
        id: loadingDialog;
        title: "Loading";
        message: "Please wait...";
        textAlign: Text.AlignHCenter;
        progress: 0;
        modalBackgroundColor: "#ffffff";
    }

    Dialog {
        id: savingDialog;
        title: "Saving";
        message: "Please wait...";
        textAlign: Text.AlignHCenter;

        modalBackgroundColor: "#ffffff";
    }

    Dialog {
        id: progressDialog;

        title: Krita.ProgressProxy.taskName != "" ? Krita.ProgressProxy.taskName : "Applying...";
    }

    Dialog {
        id: modifiedDialog;
        title: "Image was modified";
        message: "The image was modified. Do you want to save your changes?";

        buttons: [ "Save", "Discard", "Cancel" ];

        onButtonClicked: {
            switch(button) {
                case 0: {
                    if (Settings.temporaryFile) {
230
                        d.saveRequested = true;
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
                        pageStack.push( saveAsPage, { view: sketchView, updateCurrentFile: false } );
                    } else {
                        savingDialog.show("Please wait...");
                        sketchView.save();

                        if (d.closeRequested) {
                            d.closeWindow();
                        } else {
                            d.loadNewFile();
                        }
                    }
                }
                case 1: {
                    if (d.closeRequested) {
                        d.closeWindow();
                    } else {
                        d.loadNewFile();
                    }
                }
                default: {
                    Settings.currentFile = d.previousFile;
                    d.saveRequested = false;
                    d.closeRequested = false;
                }
            }
        }

        onCanceled: {
            Settings.currentFile = d.previousFile;
            d.saveRequested = false;
            d.closeRequested = false;
        }
    }

    Connections {
        target: Krita.ProgressProxy;

        onTaskStarted: progressDialog.show();
        onTaskEnded: progressDialog.hide();

        onValueChanged: progressDialog.progress = value;
    }

    Connections {
        target: Settings;
        onTemporaryFileChanged: if (window.temporaryFile !== undefined) window.temporaryFile = Settings.temporaryFile;
    }

    Connections {
        target: Krita.Window;

        onCloseRequested: {
            if (sketchView.modified) {
                d.closeRequested = true;
                modifiedDialog.show();
            } else {
                d.closeWindow();
            }
        }
    }

    Component.onCompleted: {
        Krita.Window.allowClose = false;
        loadingDialog.show("Please wait...");
295 296 297 298

        if(Settings.currentFile.indexOf("temp") == -1) {
            sketchView.file = Settings.currentFile;
        }
299 300
    }

301
    Component { id: openImagePage; OpenImagePage { onFinished: { pageStack.pop(); d.beginOpenFile(file); } } }
302 303
    Component { id: settingsPage; SettingsPage { } }
    Component { id: helpPage; HelpPage { } }
304
    Component { id: saveAsPage; SaveImagePage { onFinished: { pageStack.pop(); d.saveFileAs(file, type); } } }
305
    Component { id: customImagePage; CustomImagePage { onFinished: { pageStack.pop(); d.beginCreateNewFile(options); } } }
306 307 308 309 310 311 312 313

    QtObject {
        id: d;

        property string previousFile;
        property bool closeRequested;
        property bool saveRequested;

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        property variant newFileOptions;
        property string fileToOpen;

        function beginCreateNewFile(options) {
            if(options !== undefined) {
                newFileOptions = options;
                if (sketchView.modified) {
                    modifiedDialog.show();
                } else {
                    d.loadNewFile();
                }
            } else {
                pageStack.push(customImagePage);
            }
        }

        function beginOpenFile(file) {
331
            if(!Settings.temporaryFile && file === sketchView.file)
332 333
                return;

334 335 336 337 338 339 340 341 342 343 344 345
            if(file !== "") {
                fileToOpen = file;
                if(sketchView.modified) {
                    modifiedDialog.show();
                } else {
                    d.loadNewFile();
                }
            } else {
                pageStack.push(openImagePage);
            }
        }

346
        function loadNewFile() {
347
            saveRequested = false;
348 349
            loadingDialog.progress = 0;

350 351
            if(newFileOptions !== undefined) {
                loadingDialog.show("Creating new image...");
352 353 354 355
                if(newFileOptions.template !== undefined) {
                    Settings.currentFile = Krita.ImageBuilder.createImageFromTemplate(newFileOptions);
                    settings.temporaryFile = true;
                } else if(newFileOptions.source === undefined) {
356 357 358 359 360 361 362 363 364 365 366
                    Settings.currentFile = Krita.ImageBuilder.createBlankImage(newFileOptions);
                    Settings.temporaryFile = true;
                } else if(newFileOptions.source == "clipboard") {
                    Settings.currentFile = Krita.ImageBuilder.createImageFromClipboard();
                    Settings.temporaryFile = true;
                }
            } else {
                loadingDialog.show("Loading " + fileToOpen);
                Settings.currentFile = fileToOpen;
                sketchView.file = Settings.currentFile;
            }
367
            menuPanel.collapsed = true;
368 369
            fileToOpen = "";
            newFileOptions = null;
370 371
        }

372 373 374 375 376 377 378 379
        function saveFileAs(file, type) {
            savingDialog.show("Saving image to " + file);

            sketchView.saveAs( file, type );

            Settings.temporaryFile = false;
        }

380 381 382 383 384 385 386
        function closeWindow() {

            Krita.Window.allowClose = true;
            Krita.Window.closeWindow();
        }
    }
}