Skip to content

Commit

Permalink
Edit folder name
Browse files Browse the repository at this point in the history
  • Loading branch information
datluong committed Aug 1, 2013
1 parent cf636b6 commit 50e2a7f
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 20 deletions.
1 change: 1 addition & 0 deletions Writer.pro
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
APP_NAME = Writer

CONFIG = qt warn_on cascades10
LIBS = -lbbdevice

include(config.pri)

Expand Down
77 changes: 66 additions & 11 deletions assets/DocumentBrowser.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 7,9 @@ Page {
// the path for browsing. Path must start with '/', with root path is '/'
// format: /[..]/[..]
property string documentPath: '/';
property bool documentBrowserInitialized: false;

signal folderNameChanged();

Container {
layout: StackLayout {}
Expand All @@ -29,21 32,26 @@ Page {
// textStyle.fontWeight: FontWeight.W600
// horizontalAlignment: HorizontalAlignment.Center
// }

TextField {
id: titleTextField
enabled: true
enabled: false
textStyle.fontWeight: FontWeight.W600
text: "WRITER"
implicitLayoutAnimationsEnabled: false
textStyle.textAlign: TextAlign.Center
clearButtonVisible: false
backgroundVisible: false
focusHighlightEnabled: false
onTextChanged: {
actionUpdateFolderName();
onTextChanged: {
if (documentBrowserInitialized)
actionUpdateFolderName();
}
}

TextField {
// this dummy textfield will make titleTextField loses focus on ENTER key
visible: false
}
} // end Title Container
Container {
//seperator
Expand Down Expand Up @@ -178,7 186,7 @@ Page {
actionOpenFile( entry, {focusEditor:true} );
}
else if (entry.type == 'folder' ) {
actionOpenFolder( entry );
actionOpenFolder( entry, {} );
}
}

Expand Down Expand Up @@ -284,6 292,9 @@ Page {
updateTitle();
}

/**
* Bind documentPath to title field
**/
function updateTitle() {
var comps = documentPath.split('/');
if (comps.length > 0) {
Expand All @@ -294,18 305,37 @@ Page {
}
}

function actionOpenFolder( folderInfo ) {
function actionOpenFolder( folderInfo, options ) {
if ( folderInfo.type != 'folder' )
return;
if (options === null || options === undefined)
options = {};

console.log('[DocumentBrowser]actionOpenFolder:',folderInfo.path);
var relativePath = writerApp.relativePath( folderInfo.path );
console.log('[DocumentBrowser]relativePath',relativePath);

var newBrowser = documentBrowserPageDef.createObject();
newBrowser.documentPath = relativePath;
newBrowser.reloadDirectory();

rootNavigationPane.push(newBrowser);
newBrowser.reloadDirectory();
newBrowser.enableTitleEditing();

newBrowser.folderNameChanged.connect(onSubfolderNameChanged);
rootNavigationPane.push(newBrowser);
newBrowser.documentBrowserInitialized = true;

if (options.editFolderName == true) {
newBrowser.beginEditFolder();
}
}

function enableTitleEditing() {
titleTextField.enabled = true;
}

function beginEditFolder() {
titleTextField.editor.setSelection( 0, titleTextField.text.length );
titleTextField.requestFocus();
}

/**
Expand Down Expand Up @@ -355,6 385,10 @@ Page {
function onDocumentTitleUpdated( newTitle ) {
console.log('onDocumentTitleUpdated', newTitle);
reloadDirectory();
}

function onSubfolderNameChanged() {
reloadDirectory();
}

/**
Expand All @@ -366,7 400,7 @@ Page {
reloadDirectory();

// open folder
actionOpenFolder( newFolder );
actionOpenFolder( newFolder, {editFolderName:true} );
}

/**
Expand All @@ -384,8 418,29 @@ Page {
actionOpenFile( newFile, {focusTitle:true, clearTitle:true} );
}

/**
* {event_handler}
* Fired when the titlebar's text is changed
*/
function actionUpdateFolderName() {

if (documentPath === '' || documentPath == '/') return;
var newName = writerApp.correctFileName( titleTextField.text.trim() );
if (newName.length === 0) {
updateTitle();
return;
} else {
var entry = writerApp.renameFolder( documentPath, newName );
if (entry.hasOwnProperty('name')) {
documentPath = entry.relativePath;
updateTitle();
folderNameChanged();
}
else {
//fail
updateTitle();
return;
}
}
}

function showMessageToast( message ) {
Expand Down
12 changes: 11 additions & 1 deletion assets/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 44,17 @@ NavigationPane {
}
}
onPopTransitionEnded: {
console.log( '[main]OnPopTransitionEnded', page );
console.log( '[main]OnPopTransitionEnded', page );
// if (rootNavigationPane.count() > 0) {
// var topPage = rootNavigationPane.at( rootNavigationPane.count() - 1);
// if (topPage.hasOwnProperty('glMetaData')) {
// var meta = topPage.glMetaData();
// if (meta.pageType == 'documentBrowser') {
// topPage.reloadDirectory();
// }
// }
// }

if ( page.hasOwnProperty('glMetaData') ) {
var meta = page.glMetaData();
if (meta.pageType == 'editor') {
Expand Down
61 changes: 56 additions & 5 deletions src/writely/WriterUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 5,8 @@
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

#include <bb/device/HardwareInfo>

#include <QDir>
#include <QFileInfoList>
#include <QFileInfo>
Expand Down Expand Up @@ -64,21 66,21 @@ void WriterUI::initializeAutosave() {
* @param documentPath start with "/"
*/
QVariantMap WriterUI::createEmptyFolder( QString documentPath ) {
QString destPath = QDir::homePath() "/documents" documentPath;
QString destPath = documentsFolderPath() documentPath;
qDebug() << "WriterUI::createEmptyFolder" << ":documentPath:" << documentPath << "destPath:" << destPath;
QDir dir(destPath);
if (!dir.exists()) return QVariantMap();

int counter = 0;
while (true) {
QString folderName = untitledFolderPath(destPath, counter);
QString folderName = genFolderPath(destPath, counter);
QDir newFolder(folderName);
if (!newFolder.exists())
break;
counter ;
}

QString newFolderName = untitledFolderPath(destPath, counter);
QString newFolderName = genFolderPath(destPath, counter);
QDir newFolder( newFolderName );

if (!newFolder.mkdir( newFolderName ))
Expand All @@ -93,6 95,47 @@ QVariantMap WriterUI::createEmptyFolder( QString documentPath ) {
return entry;
}

/**
* Rename an folder and return the folder info.
* An empty QVariant is returned if there's an error
*/
QVariantMap WriterUI::renameFolder( QString relativeFolderPath, QString newName ) {
QString destPath = documentsFolderPath() relativeFolderPath;
qDebug() << "WriterUI::renameFolder" << destPath << " -> " << newName;
QFileInfo folderInfo(destPath);
QDir folder(destPath);
if (!folder.exists()) return QVariantMap();
QDir parentFolder(destPath);
parentFolder.cdUp();
qDebug() << "parentFolder" << parentFolder.path();

int counter = 0;
while (true) {
QString folderName = genFolderPath(parentFolder.path(), counter, newName);
QDir newFolder(folderName);
if (!newFolder.exists())
break;
counter ;
}
QString newFolderName = genFolderPath(parentFolder.path(), counter, newName);
if ( !folder.rename( destPath, newFolderName ) )
return QVariantMap();

QFileInfo info(newFolderName);
if ( !info.exists() )
return QVariantMap();

QVariantMap entry;
entry["type"] = "folder";
entry["name"] = info.baseName();
entry["path"] = info.filePath();
entry["relativePath"] = relativePath( info.filePath() );

qDebug() << "WriterUI::renameFolder:reesult:" << entry;
return entry;

}

/**
* Create an empty file and return the new path.
* @return empty map if the new document couldn't be created in the specified path
Expand Down Expand Up @@ -130,8 173,11 @@ QVariantMap WriterUI::createEmptyFile( QString documentPath ) {
return entry;
}

QString WriterUI::untitledFolderPath(QString path, int counter) {
QString folderName = path "/Untitled Folder";
/**
* return fullPath
*/
QString WriterUI::genFolderPath(QString path, int counter, QString defaultName ) {
QString folderName = path "/" defaultName;
if (counter > 0)
folderName = QString(" %1").arg(counter);
return folderName;
Expand Down Expand Up @@ -393,6 439,11 @@ Page* WriterUI::currentEditorPage() {
return NULL;
}

bool WriterUI::isPhysicalKeyboardDevice() {
bb::device::HardwareInfo hwInfo;
return hwInfo.isPhysicalKeyboardDevice();
}

void WriterUI::onAppAboutToQuit() {
qDebug() << "WriterUI::onAppAboutToQuit()";
// qDebug() << "Pages Count:" << mRootNavigationPane->count();
Expand Down
9 changes: 6 additions & 3 deletions src/writely/WriterUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 37,17 @@ class WriterUI : public QObject
Q_INVOKABLE QVariantMap createEmptyFile( QString documentPath );
Q_INVOKABLE bool deleteFile(QString filePath);
Q_INVOKABLE bool deleteFolder(QString folderPath);
Q_INVOKABLE QVariantMap renameFolder( QString relativeFolderPath, QString newName );
Q_INVOKABLE QString relativePath(QString filePath);

Q_INVOKABLE bool isPhysicalKeyboardDevice();
Q_INVOKABLE QString correctFileName(const QString& fileName);
private:
NavigationPane* mRootNavigationPane;

QString correctFileName(const QString& fileName);
QString untitledFilePath(const QString& path, int counter);
QString untitledFolderPath(QString destPath, int counter);
QString genFolderPath(QString path, int counter, QString defaultName = QString("Untitled Folder") );


QString availableUntitledFilePath( const QString& path, const QString& originalPath );
QString documentsFolderPath();

Expand Down

0 comments on commit 50e2a7f

Please sign in to comment.