1/**************************************************************************
2 **                                                                      **
3 ** Copyright (C) 2018 Lukas Spies                                       **
4 ** Contact: http://photoqt.org                                          **
5 **                                                                      **
6 ** This file is part of PhotoQt.                                        **
7 **                                                                      **
8 ** PhotoQt is free software: you can redistribute it and/or modify      **
9 ** it under the terms of the GNU General Public License as published by **
10 ** the Free Software Foundation, either version 2 of the License, or    **
11 ** (at your option) any later version.                                  **
12 **                                                                      **
13 ** PhotoQt is distributed in the hope that it will be useful,           **
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of       **
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        **
16 ** GNU General Public License for more details.                         **
17 **                                                                      **
18 ** You should have received a copy of the GNU General Public License    **
19 ** along with PhotoQt. If not, see <http://www.gnu.org/licenses/>.      **
20 **                                                                      **
21 **************************************************************************/
22
23import QtQuick 2.5
24import QtQuick.Controls 1.4
25
26import "../elements"
27import "../handlestuff.js" as Handle
28
29Item {
30
31    id: rename_top
32
33    x: 0
34    y: (container.height-height-110)/2
35    width: container.width-110
36    height: Math.min(container.height, childrenRect.height)
37
38    // Heading
39    Text {
40        id: headingtext
41        text: em.pty+qsTr("Rename File")
42        color: colour.text
43        font.bold: true
44        font.pointSize: 18*2
45        x: (parent.width-width)/2
46        anchors.top: rename_top.top
47        anchors.topMargin: 25
48    }
49
50    // The filename is dynamically updated when element is shown
51    Text {
52        id: filename
53        text: "/path/to/some/image/file.jpg"
54        color: colour.text_inactive
55        font.pointSize: 10*2
56        x: (parent.width-width)/2
57        anchors.top: headingtext.bottom
58        anchors.topMargin: 25
59    }
60
61    // The new filename (the suffix cannot be changed here)
62    Rectangle {
63        id: newfilenamecontainer
64        color: "#00000000"
65        width: childrenRect.width
66        height:childrenRect.height
67        x: (parent.width-width)/2
68        anchors.top: filename.bottom
69        anchors.topMargin: 25
70        Row {
71            spacing: 5
72            CustomLineEdit {
73                id: newfilename
74                enabled: management_top.opacity===1&&management_top.current==="rn"
75                text: ""
76                fontsize: 13
77                width: rename_top.width/2
78            }
79            Text {
80                id: suffix
81                color: colour.text
82                text: ".JPG"
83                font.pointSize: 13
84            }
85        }
86    }
87
88    // The two buttons for save/cancel
89    Rectangle {
90        color: "#00000000"
91        width: childrenRect.width
92        height:childrenRect.height
93        x: (parent.width-width)/2
94        anchors.top: newfilenamecontainer.bottom
95        anchors.topMargin: 25
96        Row {
97            spacing: 5
98            CustomButton {
99                // Button for renaming the current file based on the entered text
100                text: em.pty+qsTr("Save")
101                fontsize: 18
102                enabled: newfilename.getText() !== ""
103                onClickedButton: {
104                    verboseMessage("Rename","Save")
105                    if(newfilename.getText() !== "")
106                        simulateEnter()
107                }
108            }
109            CustomButton {
110                text: em.pty+qsTr("Cancel")
111                fontsize: 18
112                onClickedButton: {
113                    verboseMessage("Rename","Cancel")
114                    management_top.hide()
115                }
116            }
117        }
118    }
119
120    Connections {
121        target: container
122        onItemShown:
123            setupRename()
124    }
125
126    Connections {
127        target: call
128        onShortcut: {
129            if(management_top.visible && current == "rn") {
130                if(sh == "Enter" || sh == "Return")
131                    simulateEnter()
132            }
133        }
134    }
135
136    // This 'simulate' function can be called via shortcut
137    function simulateEnter() {
138        verboseMessage("FileManagement/Rename", "simulateEnter()")
139        if(newfilename.getText() !== "") {
140            // a rename is the same as a move into the same directory
141            getanddostuff.moveImage(variables.currentDir + "/" + variables.currentFile, variables.currentDir + "/" + newfilename.getText() + suffix.text)
142            Handle.loadFile(variables.currentDir + "/" + newfilename.getText() + suffix.text, variables.filter, true)
143            management_top.hide()
144        }
145    }
146
147    function setupRename() {
148        verboseMessage("FileManagement/Rename", "setupRename()")
149        filename.text = variables.currentFile
150        newfilename.text = ""	// This is needed, otherwise the lineedit might keep its old contents
151                                // (if opened twice for same image with different keys pressed in between)
152        newfilename.text = getanddostuff.getImageBaseName(variables.currentFile)
153        suffix.text = "." + getanddostuff.getSuffix(variables.currentFile)
154        newfilename.selectAll()
155    }
156
157}
158